Tutorial :uint64 flag or uint32 flag[2] for function argument to 32bit compiler?


I have a uint32 variable for some bit field flag state and an enum for these flags. This variable is passed as argument to some functions by value.

I need to add about 20 more flags to it. What are my best options? Extend it to 64 bits or treat it as an array of 2 32bits?

Extending to 64 bits will lead me to use compiler 'extensions', as enum is 32bits wide. But using an array seems to lead to more work.

My code compiles on 32 and 64 bit compilers, and runs on windows, linux and mac.

Thanks in advance.


I would use an STL bitset. Then my enum wouldn't have to be a power of 2. With normal enumeration, it would just be the index into the bitset. You could also use a vector<bool>.

The chief benefit would be portability since your flag set would now fit in any platform's normal enum range.


enum errorFlags { parity, framing, overrun, errorFlags_size };    std::bitset<errorFlags_size> flagSet;    . . .    // either of these set framing error condition  flagSet[framing] = true;  flagset.set(framing);    // either of these clear the framing error condition  flagSet[framing] = false;  flagset.reset(framing);    // this tests for the framing error condition  if (flagSet[framing])  {      // we have a framing error  }  


Do you know about the bitfield syntax? It looks like this:

struct Flags {      unsigned int flag1 : 1;      unsigned int flag2 : 1;      unsigned int flag3 : 1;  }  

And so on. The number after the colon indicates the number if bits you want to dedicate to the member. This is standard C++, not some weird vendor extension.

More at http://msdn.microsoft.com/en-us/library/ewwyfdbe(VS.71).aspx.


I would reccomend that you use std::bitset instead.


As you are using C++, you could write a class as a replacement, internally it could store two 32 bit integers.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »