creating and using bit flags..?
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++
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:
HTH,
Pat
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
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....
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; }};
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.
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....
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.
alright well im gonna put this bad boy to work...
for(int i = 0; i < 999999999; i++)
{
thanks!!!!!!!!!!!!!!!;
}
^^ sorry about that... im just excited
for(int i = 0; i < 999999999; i++)
{
thanks!!!!!!!!!!!!!!!;
}
^^ sorry about that... im just excited
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!
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement