How do I create my own set of flags?
I want to use a set of flag identifiers, in a DWORD, in one of my structs. Each flag is a 1-bit boolean in the DWORD. You know; like the ones in DDSCAPS and the Blt() functions. I''m not sure how to create and define my own flag set. Could anyone fill me in?
No, Canadians DON'T all live in igloos. They live in one BIG igloo!
Pretty simple really, you just need to define a set of constants whose values are all exact powers of 2 (so that their value in binary is all zeros except for a single 1 somewhere in the sequence).
Here's an example :
This can also be written with the values in hex :
If you're only ever using C++ then it's more correct to do it with actual typed constants instead of using #define statements. Like this :
Does that make sense?
Iain Hutchison
Programmer, Silicon Dreams
The views expressed here are my own, not those of my employer.
[EDIT : source-code highliting seems to be going a bit nuts - using PRE]
[edited by - pieman on April 11, 2002 10:57:20 AM]
Here's an example :
#define FLAG_BIT0 (1<<0)#define FLAG_BIT1 (1<<1)#define FLAG_BIT2 (1<<2)#define FLAG_BIT3 (1<<3)#define FLAG_BIT4 (1<<4)
This can also be written with the values in hex :
#define FLAG_BIT0 (0x00000001UL)#define FLAG_BIT1 (0x00000002UL)#define FLAG_BIT2 (0x00000004UL)#define FLAG_BIT3 (0x00000008UL)#define FLAG_BIT4 (0x00000010UL)
If you're only ever using C++ then it's more correct to do it with actual typed constants instead of using #define statements. Like this :
const DWORD FLAG_BIT0 = (1<<0);const DWORD FLAG_BIT1 = (1<<1);const DWORD FLAG_BIT2 = (1<<2);const DWORD FLAG_BIT3 = (1<<3);const DWORD FLAG_BIT4 = (1<<4);
Does that make sense?
Iain Hutchison
Programmer, Silicon Dreams
The views expressed here are my own, not those of my employer.
[EDIT : source-code highliting seems to be going a bit nuts - using PRE]
[edited by - pieman on April 11, 2002 10:57:20 AM]
Iain HutchisonProgrammer, Silicon DreamsThe views expressed here are my own, not those of my employer.
So, if I had a struct with a DWORD member variable, dwFlags, I could just fill it with these defined bit values...
MyStruct.dwFlags = FLAG_DILL|FLAG_PICKLE;
...and then would I detect if the flag was present using ''&'', right?
if(Mystruct.dwFlags & FLAG_DILL) {
//The flag is present
}
MyStruct.dwFlags = FLAG_DILL|FLAG_PICKLE;
...and then would I detect if the flag was present using ''&'', right?
if(Mystruct.dwFlags & FLAG_DILL) {
//The flag is present
}
No, Canadians DON'T all live in igloos. They live in one BIG igloo!
No, I don't think that will work. AND gives you a 1 only when both corresponding bits are 1. OR gives you a 1 if any of the corresponding bits are 1. But that doesn't happen correctly here.
The equality operator has precedence over bitwise AND. So, Mystruct.dwFlags == FLAG_DILL gets evaluated first. The result will be one of two cases: 1) boolean true , represented as non-0 (usually 1) if Mystruct.dwFlags has only the flag FLAG_DILL 2) 0 in any other case. Since, in case #1, FLAG_PICKLE will only do anything if it just so happens to be 1, in which case there will be a common bit. In case #2, 0 has no common bits with FLAG_DILL , the final result will be 0.
In other words, this just doesn't work at all like advertised. To correctly check for multiple flags within a single value, just OR the desired flags together and AND that result with the value you're testing. For example: if(Mystruct.dwFlags & (FLAG_DILL | FLAG_PICKLE)) will do the trick. You need the parenthesis around the OR'ed pair because bitwise AND has precedence over bitwise OR. So, this being said, I don't think there's any way then to test for flag values within a switch construct since it tests only equality.
[edited by - merlin9x9 on April 12, 2002 1:35:16 AM]
The equality operator has precedence over bitwise AND. So, Mystruct.dwFlags == FLAG_DILL gets evaluated first. The result will be one of two cases: 1) boolean true , represented as non-0 (usually 1) if Mystruct.dwFlags has only the flag FLAG_DILL 2) 0 in any other case. Since, in case #1, FLAG_PICKLE will only do anything if it just so happens to be 1, in which case there will be a common bit. In case #2, 0 has no common bits with FLAG_DILL , the final result will be 0.
In other words, this just doesn't work at all like advertised. To correctly check for multiple flags within a single value, just OR the desired flags together and AND that result with the value you're testing. For example: if(Mystruct.dwFlags & (FLAG_DILL | FLAG_PICKLE)) will do the trick. You need the parenthesis around the OR'ed pair because bitwise AND has precedence over bitwise OR. So, this being said, I don't think there's any way then to test for flag values within a switch construct since it tests only equality.
[edited by - merlin9x9 on April 12, 2002 1:35:16 AM]
AND gives you a 1 only when corresponding bits are 1. OR gives you a 1 if either are 1. Or gives you 1 one only if just one of the bits is 1. So, here are some examples:
I hope I did these right. Anyway, I'm sure you can see how OR joins values and AND masks them.
[edited by - merlin9x9 on April 12, 2002 1:19:10 PM]
AND:
1000101101010001
0110110100010111
----------------
0000100100010001
OR:
1000101101010001
0110110100010111
----------------
1110111101010111
XOR:
1000101101010001
0110110100010111
----------------
1111011001000110
I hope I did these right. Anyway, I'm sure you can see how OR joins values and AND masks them.
[edited by - merlin9x9 on April 12, 2002 1:19:10 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement