Skip to main content
GameDev.net gamedev.net
🔒 Locked

creating and using bit flags..?

Started by MTclip Oct 8, 2005 at 8:47 PM 12 replies 1.3k views
Original Post
MTclip
MTclip
just wondering if any one know a good example of how to create bitflag operations... I would like it to work in the same way that directx's work.. something like this createCamera( CAMERA_FPS | CAMERA_YINVERTED | CAMERA_LOWSENSITIVITY); thanks.. I am using VC++
darookie
darookie
Bit flags are generally just numbers that equal powers of two. On a standard 32 bit platform you usually have 32 of these flags at your disposal.
Here's a (very brief) overview of the common operations:

typedef unsigned long bitflag;// this little tool can be used to calculate the value of a bitflag by index (0..31)template<unsigned long N>struct bit {    static bitflag const value = 1 << N;};// sample usageenum MyFlags {   FlagOne  = bit<0>::value,   FlagTwo  = bit<1>::value,   FlagThree= bit<2>::value,};// test whether a flag is settemplate<typename T>inlinebool is_set( T bitset, bitflag flag ) {   return ( bitset & flag ) == flag;}// test whether at least one of many flags is settemplate<typename T>inlinebool contains_flag( T bitset, bitflag flags ) {   return ( bitset & flags ) > 0;}// set a bitflagtemplate<typename T>inlinevoid set_flag( T & bitset, bitflag flag ) {   bitset |= flag;}// unset a bitflag - works by masking the bitset with the binary complement (~) of the flag(s)template<typename T>inlinevoid unset_flag( T & bitset, bitflag flag ) {   bitset &= ~flag;}// sample usage:long bitset = 0;// set a single flagset_flag( bitset, FlagOne );// set many flagsset_flag( bitset, FlagOne | FlagTwo );// test for a single flagif ( is_set( bitset, FlagThree ) ) {  // ...}// see whether at least one flag is setif ( contains_flag( bitset, FlagOne | FlagThree ) ) {   // the above would evaluate to true}// clear flags (only FlagTwo will be unset)unset_flag( bitset, FlagTwo | FlagThree );


HTH,
Pat
MTclip
MTclip

ok let me make sure i got this right...


// i can have up to 32 different flagsenum MyFlags {   FlagOne  = bit<0>::value,   FlagTwo  = bit<1>::value,   FlagThree= bit<2>::value,   .   .   .    FlagThirtyTwo= bit<31>::value};/// in my desired function i would do this..void CreateCamera(long flags);{   if ( is_set( flags, FlagThree ) )   {        /// do stuff   }   .   .   .   // through the number of valid flags   if ( is_set( flags, FlagThirtyTwo ) )   {        /// do more stuff   }}


also what is the difference between is_set() and contains_flag()

thanks for that....
Jan-Lieuwe
Jan-Lieuwe
Here's a class I wrote for this purpose:

class Flags{protected:   int m_flags;public:   Flags() : m_flags(0)   {   }      void setFlags(int f)    { m_flags |= f; }   void setInvFlags(int f) { m_flags |= ~f; }   void filterFlags(int f) { m_flags &= f; }   int  testFlags(int f)   { m_flags & f; }   int  testInvFlags(int f){ m_flags & ~f; }   void resetFlags(int f)  { m_flags &= ~f; }   void invertFlags(int f) { m_flags ^= f; }   void invertAllFlags()   { m_flags = ~m_flags; }};
darookie
darookie
Quote:
Original post by MTclip

ok let me make sure i got this right...


*** Source Snippet Removed ***

also what is the difference between is_set() and contains_flag()

thanks for that....

Seems as if you got the idea [smile].
The difference between is_set and contains_flag is that is_set only returns true iff all given flags are set, while contains_flag would return true if at least one of the given flags is set (kind of like "if x or y or z").

Happy coding,
Pat.

MTclip
MTclip


I want to beable to call my function like this
CreateCamera(Flag8 | Flag30 | Flag 27 | Flag6);

so how should i declare the function..
void CreateCamer(long flags)
or
void CreateCamera(MyFlags flags)


also ..
enum MyFlags {
FlagOne = bit<0>::value,
FlagTwo = bit<1>::value,
FlagThree= bit<2>::value,
};

what is this ::value buisness why is it there?

thanks some more....
darookie
darookie
Quote:
Original post by MTclip


I want to beable to call my function like this
CreateCamera(Flag8 | Flag30 | Flag 27 | Flag6);

so how should i declare the function..
void CreateCamer(long flags)
or
void CreateCamera(MyFlags flags)

You would use the first version - void CreateCamera( long )
Quote:

also ..
enum MyFlags {
FlagOne = bit<0>::value,
FlagTwo = bit<1>::value,
FlagThree= bit<2>::value,
};

what is this ::value buisness why is it there?

The struct is a tool that lets the compiler do the calculation of the actual power-of-two bit value. Read about C++ templates if you are interested in how this works. You could also just replace the bit<i>::value parts with 1 << i or define each value directly like
static long const Bit0 = 1;static long const Bit1 = 2;static long const Bit2 = 4;...

I just find the bit::value notation a bit more descriptive, bit if that confuses you feel free to use constants [smile]

HTH,
Pat.


MTclip
MTclip
alright well im gonna put this bad boy to work...

for(int i = 0; i < 999999999; i++)
{
thanks!!!!!!!!!!!!!!!;
}

^^ sorry about that... im just excited
BeerNutts
BeerNutts
Quote:
Original post by darookie
Bit flags are generally just numbers that equal powers of two. On a standard 32 bit platform you usually have 32 of these flags at your disposal.
Here's a (very brief) overview of the common operations:

*** Source Snippet Removed ***

HTH,
Pat


O Man, C plus plus is the s**t (not)

// this little tool can be used to calculate the value of a bitflag by index (0..31)template<unsigned long N>struct bit {    static bitflag const value = 1 << N;};// sample usageenum MyFlags {   FlagOne  = bit<0>::value,   FlagTwo  = bit<1>::value,   FlagThree= bit<2>::value,};// test whether a flag is settemplate<typename T>inlinebool is_set( T bitset, bitflag flag ) {   return ( bitset & flag ) == flag;}


You're kidding right? Wow.

how about...
#define BIT(xBitToSet)  (1 << ((unsigned long)xBitToSet))#define BIT_IS_SET(xValue, xBit) (((unsigned long)xValue) & BIT(xBit))// oh no, this is too short, and makes too much sense, must use templates agghhhhh!

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
JohnBolton
JohnBolton
How about using std::bitset instead writing your own class?
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
mfawcett
mfawcett
Quote:
Original post by JohnBolton
How about using std::bitset instead writing your own class?

I was wondering the same thing, especially since it's not limited by some arbitrary number of bits like the source posted is. And if you need run-time support, there is always boost::dynamic_bitset.

--Michael Fawcett
taby
taby
The C++ standard also states that elements in a vector must only consume one bit of memory each.
gumpy
gumpy
Quote:
Original post by JohnBolton
How about using std::bitset instead writing your own class?


the standard library is a wonderful thing, indeed. std::bitset
This space for rent.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.