Advertisement

Defining dword flags

Started by June 13, 2002 04:24 PM
3 comments, last by Abort_Retry_Fail 22 years, 6 months ago
How do I create my own flag system for a certain struct? I want to store a bunch of booleans in a single dword, but I don''t know how to define my own flags for that dword.
Are you talking about bitfields ?


  struct foo_t{            bool  a : 1;   //  1 bit,  treated as bool            bool  b : 1;   //  1 bit,  treated as bool   unsigned int   c : 1;   //  1 bit,  treated as unsigned int   signed   char  d : 7;   //  7 bits, treated as signed char   signed   long  e : 10;  // 10 bits, treated as unsigned long   unsigned short f : 12;  // 12 bits, treated as unsigned short}; // total 32 bits...foo_t foo = { true, false, 0, ''a'', -2, 1000 };  


Which lets you pack a bunch of flags in a struct.

  struct foo_t{  bool bit0 : 1;  bool bit1 : 1;  bool bit2 : 1;  bool bit3 : 1;  bool bit4 : 1;  bool bit5 : 1;  bool bit6 : 1;  bool bit7 : 1;};  



Or a set of binary constants;

  const unsigned long BIT0 = 1 << 0;const unsigned long BIT1 = 1 << 1;const unsigned long BIT2 = 1 << 2;const unsigned long BIT3 = 1 << 3;const unsigned long BIT4 = 1 << 4;...unsigned long FOO = BIT0 | BIT4;  


Or a C++ bitset class (which covers most uses).


  #include <bitset>typedef std::bitset<12> flag_t; // 12-bit bitsetflag_t foo1 = "110000110101";flag_t foo2 = 255;...if ( foo1[5] ) {   foo2.flip( 4 );  foo2.reset( 2 );  foo1.set( 0 );  ...};if( foo1 == foo2 ) ...if( (foo1 & ~foo2).any() ) ...  




Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Umm... I mean like the .dwFlags member of structs like DDSURFACEDESC and DDBLITFX - they''re binary OR''ed together in a DWORD, and each flag represents a certain attribute.

Ex:
ddbltfx.dwFlags = DDBLT_COLORFILL;

How would I make my own set of flags for a certain struct to use? (If you''ve already shown me, forgive my blindness and point out which method would do this)
Ah...

You want the crude way...

*shakes his head with disgust*

Use the third code block.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Define your flags as powers of 2

#define FLAG1 1 (0x00000001 in binary)
#define FLAG2 2 (0x00000010 in binary)
#define FLAG3 4 (0x00000100 in binary)
#define FLAG4 8 (0x00001000 in binary)
etc...

Notice the pattern, they each take their own bit

Now say you want certain flags, just OR those flags together to get the final value

Then if you want to check if a certain flag is set in a DWORD of ORed flags, just do say
DWORD Flags = FLAG1 | FLAG3

if(Flags & FLAG1) ..do stuff for flag1

hope that helps

ByteMe95::~ByteMe95()
My S(h)ite
ByteMe95::~ByteMe95()My S(h)ite

This topic is closed to new replies.

Advertisement