Bit Flags Class

The more I flesh out my resource manager design the more I realize just how much more work and classes I need still need to add. While I was working on my ResourceItem class, one of the components it needs is to keep track of a bit field to keep track of its state whether it is loaded, ready to use, disabled, etc.  Instead of normally hacking my way through the code implementations i decided to encapsulate that bit field implementation instead. And I created a class that wraps bit manipulation called BitFlags. Here’s a simple UML Class Diagram below.


BitFlagsBitFlags member and interface.

Basically, this class handles the low level bitwise manipulation like setting which particular bits on and off. The interface seems self-explanatory so I’ll just let you figure the methods out. Link to the implementation is provided below. As an added bonus, here’s a bunch of bitwise manipulation macros extracted from my Pulse Library project.

  1. // Bit manipulation macros
  2. #define PSX_GetBit(x)                (1<<(x))
  3. #define PSX_TestBit(flag, bit)        (((flag) & PSX_GetBit((bit))) !=0)
  4. #define PSX_TestBitAll(flag, set)    (((flag) & (set)) == (set))
  5. #define PSX_TestBitIfAny(flag, set) (((flag) & (set)) !=0)
  6. #define PSX_SetBit(flag, bit)        ((flag) |= PSX_GetBit((bit)))
  7. #define PSX_SetAll(flag, set)        ((flag) |= (set))
  8. #define PSX_ClearBit(flag, bit)     ((flag) &= ~PSX_GetBit((bit)))
  9. #define PSX_ClearAll(flag, set)     ((flag) &= ~(set))


Link to BitFlags class implementation:

Feel fre to ask if you have any question!

– CodeSushi


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s