• Advertisement
Sign in to follow this  

creating and using bit flags..?

This topic is 4518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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++

Share this post


Link to post
Share on other sites
Advertisement
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 usage
enum MyFlags {
FlagOne = bit<0>::value,
FlagTwo = bit<1>::value,
FlagThree= bit<2>::value,
};

// test whether a flag is set
template<typename T>
inline
bool is_set( T bitset, bitflag flag ) {
return ( bitset & flag ) == flag;
}

// test whether at least one of many flags is set
template<typename T>
inline
bool contains_flag( T bitset, bitflag flags ) {
return ( bitset & flags ) > 0;
}

// set a bitflag
template<typename T>
inline
void 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>
inline
void unset_flag( T & bitset, bitflag flag ) {
bitset &= ~flag;
}

// sample usage:
long bitset = 0;

// set a single flag
set_flag( bitset, FlagOne );
// set many flags
set_flag( bitset, FlagOne | FlagTwo );

// test for a single flag
if ( is_set( bitset, FlagThree ) ) {
// ...
}

// see whether at least one flag is set
if ( 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

Share this post


Link to post
Share on other sites

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



// i can have up to 32 different flags
enum 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....

Share this post


Link to post
Share on other sites
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; }

};

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites


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....

Share this post


Link to post
Share on other sites
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<N>::value notation a bit more descriptive, bit if that confuses you feel free to use constants [smile]

HTH,
Pat.


Share this post


Link to post
Share on other sites
alright well im gonna put this bad boy to work...

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

^^ sorry about that... im just excited

Share this post


Link to post
Share on other sites
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 usage
enum MyFlags {
FlagOne = bit<0>::value,
FlagTwo = bit<1>::value,
FlagThree= bit<2>::value,
};

// test whether a flag is set
template<typename T>
inline
bool 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!



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by BeerNutts
O Man, C plus plus is the s**t (not)
*


It is not. C++ is pretty usefull tool which in many cases lets you write more stable, clear and robust code. Actually, this example with templated struct IS indeed, IMO, stupid (it is 1 minute to make b00, b01, b02 [...] static const variables in header for bits - less typing later in code and more readability). As for macros, you shouldn't use them at all - they are evil.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
The C++ standard also states that elements in a vector<bool> must only consume one bit of memory each.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement