|
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
I used to do this way......
#include <iostreamᡊe9ac73-238b-46be-991d-0f050c37248dusing namespace std;
enum ST { enType1 = 0x01, enType2 = 0x02, enType3 = 0x03 };
void SetVar(int style)
{
int tt;
for(tt = enType1; tt <= enType3; tt = (tt << 1))
{
if(style & tt)
cout << "Type " << tt / 2 + 1 << " implemented\n";
}
}
void main()
{
SetVar(enType1 | enType3);
}
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
Thanks.
Please read my reply above.
Kuphryn
|
|
|
|
|
I would not use an enum, but rather just a set of defines for every flag
#define FEATURE1 0x00000001 // bit one
#define FEATURE2 0x00000002 // bit two
#define FEATURE3 0x00000004 // bit three
#define FEATURE4 0x00000008 // bit four
#define FEATURE5 0x00000010 // bit five
.
.
. This will give you up to 32 flags.
Then, the parameter used to set the flags would be a DWORD
void SetVariableA (DWORD dwflags = 0);
You would call it this way
SetVariableA (FEATURE1 | FEATURE3);
Then, inside SetVariableA() you would check to see which flags have been set by using the bitwise AND ( & ) operator
void SetVariableA (DWORD dwFlags)
{
if (dwFlags & FEATURE1)
{
}
if (dwFlags & FEATURE2)
{
.
.
.
HTH
---
CPUA 0x5041
Sonork 100.11743 Chicken Little
If a man is standing in the middle of the forest speaking and there is no woman around to hear him...is he still wrong?
|
|
|
|
|
Thanks.
You, Ravi Bhavnani, and Maxwell Chen posted three very similar techniques. All seems important. Your technique, however, make use of #define instead of enum and DWORD instead of UINT.
1) Can you explain #define over enum and DWORD of UINT?
2) Can I use #define FEATURE1 1, #define FEATURE2 2, etc.. instead of hex?
3) I asked Maxwell Chen about calling the FEATURE1 *outside* of the class.
For example:
-----
int main()
{
...
SetVariableA(FEATURE1);
-----
How does mean know what FEATURE1, FEATURE2, etc. are?
Last, this question applies to everyone. In the function,
// SetVariableA (FEATURE1 | FEATURE3);
I believe that means you want FEATURE1 and you want FEATURE3. However, in the implementation of SetVariableA(UINT) or SetVariableA(DWORD), there are cases for (dwFlags & FEATURE1), (dwFlags & FEATURE2), etc. How about something like:
// FEATURE1 | FEATURE3
Oh. I think I know what is going on. For every "if cases" the statement sets *each* FEATURE accordingly. For example:
// SetVariableA (FEATURE1 | FEATURE3);
// SetVariableA(UINT flags) or SetVariableA(DWORD flags)
{
if (flags & FEATURE1) // applying to the call above, YES
if (flags & FEATURE2) // NO
if (flags & FEATURE3) // YES
}
Am I on track?
Kuphryn
|
|
|
|
|
kuphryn wrote:
1) Can you explain #define over enum and DWORD of UINT?
with an enum, you can loop over the values, so technically it probably better, but I prefer #defines, They are easier IMO
On 32 bit systems, there is no difference between UINT and DWORD. On 16 bit systems UINT is 16 bits, DWORD is 32 bits
kuphryn wrote:
2) Can I use #define FEATURE1 1, #define FEATURE2 2, etc.. instead of hex?
Yes you can, but remember that you are setting the individual bits, so your values would be 1, 2, 4, 8, 16, 32, 64, 128 etc. using hex is just easier IMO.
just remember that the bitwise OR operator, when used with properly defined flags, acts as a plus operator.
ie :
1 | 2 == 3
1 | 4 == 5
2 | 4 == 6
1 | 2 | 4 == 7
...
kuphryn wrote:
How does mean know what FEATURE1, FEATURE2, etc. are
that is why I put the defines in the header file. Just include the header file in which ever file you use the flags.
kuphryn wrote:
Am I on track?
Yes. Be sure to read up on bitwise operators in MSDN.
HTH
---
CPUA 0x5041
Sonork 100.11743 Chicken Little
If a man is standing in the middle of the forest speaking and there is no woman around to hear him...is he still wrong?
|
|
|
|
|
I wrote something wrong, so I deleted it.
#define preprocessor directive affetcs everywhere after the compiler "sees" it.
Enum is something like you are creating some integral type. Those members of a certain enum type, you may take them as integral number, like int, short.
The compiler optimizes the size, if all members in a enum type are less than maximum value of short, then they are short. Else they are taken as int or long... If beyond maximum of (unsigned) int or long, you get a error compiling msg!
Bjarne recommends C++ programmers less using MACROs.
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
Okay. Thanks everyone.
I will start out using #define and DWORD.
Kuphryn
|
|
|
|
|
You can see the value of an enum in the debugger. With a define you cannot.
typedef enum
{
FEATURE1 = 0x0001,
FEATURE2 = 0x0002,
FEATURE3 = 0x0004,
FEATURE4 = 0x0008,
FEATURE5 = 0x0010,
} FEATURES;
Todd Smith
|
|
|
|
|
I want to make sure I have the idea behind hex done. Here is the 32 flags using hex.
#define 0x0000001
#define 0x0000002
#define 0x0000004
#define 0x0000008
#define 0x0000010
#define 0x0000020
#define 0x0000040
#define 0x0000060
#define 0x0000080
#define 0x0000100
#define 0x0000200
#define 0x0000400
#define 0x0000600
#define 0x0000800
#define 0x0001000
#define 0x0002000
#define 0x0004000
#define 0x0006000
#define 0x0008000
#define 0x0010000
#define 0x0020000
#define 0x0040000
#define 0x0060000
#define 0x0080000
#define 0x0100000
#define 0x0200000
#define 0x0400000
#define 0x0600000
#define 0x0800000
#define 0x1000000
#define 0x2000000
#define 0x4000000
#define 0x6000000
#define 0x8000000
etc...
Does the above fit the pattern of 1, 2, 4, 6, 8, 16,... that PJ Arends mentioned?
Kuphryn
|
|
|
|
|
Oh boy, that makes your finger tired...
Don't code too much!
Use this:
#define BIT_MASK(n) (0x1 << (n))
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
Thanks.
// #define BIT_MASK(n) (0x1 << (n))
Ahhh. How many bits does the << operator add to the hex?
-----
#define BIT_MASK(0) (0x1 << (0)) = 0x1
#define BIT_MASK(1) (0x1 << (1)) = 0x2
#define BIT_MASK(2) (0x1 << (2)) = 0x4
#define BIT_MASK(3) (0x1 << (3)) = 0x6
// According to PJ Arends and Michael Dunn, there is no 6 bits
...
Kuphryn
|
|
|
|
|
The bit-wise operator << shifts the amount of bits depending on the number after this operator. Therefore,
0x1 (binary 00000001) shifts 1 position you get 0x2 (binary is 00000010).
0x3 (binary 00000011) shifts 3 position you get 0x18 (binary is 00011000).
kuphryn wrote:
#define BIT_MASK(3) (0x1 << (3)) = 0x6
What you wrote this is wrong! Should be 0x8.
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
|
Please read my new article at http://www.codeproject.com/cpp/bitbashing.asp. It was inspired by this thread, and I hope it will help you a lot.
---
CPUA 0x5041
Sonork 100.11743 Chicken Little
If a man is standing in the middle of the forest speaking and there is no woman around to hear him...is he still wrong?
|
|
|
|
|
Very nice! Thanks.
I do have some questions. However, I have late for class, so I will ask these questions later on today.
Kuphryn
|
|
|
|
|
|
|
I was wondering if there is anyway to add a specific selection to the system menu in the title bar?
|
|
|
|
|
Do this in OnInitDialog() :
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL) {
pSysMenu->AppendMenu(MF_STRING, IDC_MYCOMMAND, "My Command"};
}
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
You can use the GetSystemMenu function on the window that you would like to add the menu selection. This will get you the handle to the system menu for that window.
Then you can use all of the menu modification functions on that handle like InsertMenuItem and AddMenuItem.
Good Luck!
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
Perfect. Thanks. Quick and painless
|
|
|
|
|
I have a pdf file. Currently, I xerox an handful
of blank forms and I just run them through my
printer to print new text on top of the form.
Is there a way to programmatically print text
on top of a pdf file (so that I can then send that
new file to the printer and get nice looking output)?
Please, any response any one can give me will be
greatly appreciated.
Sincerely,
Danielle (an overworked graduate student)
|
|
|
|
|
I don't know how to do this programatically, but recent versions of Adobe Acrobat Writer lets you create PDF forms that can be filled in by a user and printed from Acrobat.
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
ok... i have an array that its size always changes... like
array[inc];
array is the array ofcource and inc //incriment.
the program allows you to add a number to a list of numbers... then you press 'p', and you would print the number list.
what i want to do is make it for if you press 's' then the program would sort all the data in array[] to least to greatest.
i have s in a switch of menu "which is a char" so...
case 's' :
{
... //least to greates program
}
how would i accomplish this?
thanks!
~SilverShalkin
|
|
|
|