GL : Mouse Position to 3d Space

JA : Getting Started
All : Qboolean
All : Bit Flags
All : Vector Math
JKA : Saber Cycle BugFix
JK2 : Classes
JK2 : Ladders
All : FontDat File Info
JK2 : Breakdown
JK2 : PM_ Anim

JK2 : Source
JKA : Source

LF : JA Coding
LF : OJP Forum
LF : General Editing
Stanford CS Library


You are visitor #140035.
(since 11/01/2005).
You are unique visitor #32745.
(since 11/01/2005).
Flags - Wudan (with many corrections to code by Trimbo)

Flags are an effective way to do some interesting stuff - like setting multiple conditions at once, whereas you might otherwise get in to a boolean shouting match.  Because of their naturally clever nature, deployed properly, flags are a must for any complex program.

Here's a table with the 32 possible individual flags, listed as the binary (so you can see what it is) and the hexidecimal:
               32 bit Flags
Binary                           Hexidecimal
00000000000000000000000000000001 0x00000001
00000000000000000000000000000010 0x00000002
00000000000000000000000000000100 0x00000004
00000000000000000000000000001000 0x00000008
00000000000000000000000000010000 0x00000010
00000000000000000000000000100000 0x00000020
00000000000000000000000001000000 0x00000040
00000000000000000000000010000000 0x00000080
00000000000000000000000100000000 0x00000100
00000000000000000000001000000000 0x00000200
00000000000000000000010000000000 0x00000400
00000000000000000000100000000000 0x00000800
00000000000000000001000000000000 0x00001000
00000000000000000010000000000000 0x00002000
00000000000000000100000000000000 0x00004000
00000000000000001000000000000000 0x00008000
00000000000000010000000000000000 0x00010000
00000000000000100000000000000000 0x00020000
00000000000001000000000000000000 0x00040000
00000000000010000000000000000000 0x00080000
00000000000100000000000000000000 0x00100000
00000000001000000000000000000000 0x00200000
00000000010000000000000000000000 0x00400000
00000000100000000000000000000000 0x00800000
00000001000000000000000000000000 0x01000000
00000010000000000000000000000000 0x02000000
00000100000000000000000000000000 0x04000000
00001000000000000000000000000000 0x08000000
00010000000000000000000000000000 0x10000000
00100000000000000000000000000000 0x20000000
01000000000000000000000000000000 0x40000000
10000000000000000000000000000000 0x80000000
Usually, flags are set in enums in header files, but I usually just straight define them. To set up a flag for later use, just drop a macro:
#define PINK_FLOWERS 0x00000001
#define RED_ROSES    0x00000002
I recommend using the hexadecimal in your defines, but you can use the decimal - doesn't matter because once you have a #define set up, you'll use the 'PINK_FLOWERS' instead of the hex representation.

So, now you have flag defines, you'll need something to 'add' them to -
int flags;
Generally, you'll want to initialize this int to 0 -
flags = 0;
This has the effect of specifying that none of the above defined flags are set.

Condition Granting

Ok, so you've got a 'flag' int (that stores the flag info), how do you set flags?  Easy.
flags |= PINK_FLOWERS;  //or
flags = flags | PINK_FLOWERS;
You can even set multiple flags at once:
Everything on the right of the |= evaluates first, so PINK_FLOWERS | RED_ROSES is 0x00000003, and our flags variable now has both flags set!

Condition Checking

How do we see if a flag is set - say if we want to check for a condition using a while or if statement?  Again, pretty simple stuff.
if( flags & PINK_FLOWERS ) //checks to see if the pink flowers flag is set
    //if it is, the condition evaluates non zero,
    //and the condition is met
Pitfall - remember how we set two flags at once before?  You might think that's a good way to check for multiple flags at once, and it does work - kind of ...  However:
if( flags & ( PINK_FLOWERS | REDROSES ))
//evaluates the return value of flags & 0x00000003
    //however, this will be non-zero (effectively true)
    //if EITHER flag is set - which could be ok


It came up that I need to teach you how to unset flags - this is pretty easy (thanks goes to DiCeS for setting me straight on the subject):
if( flags & PINK_FLOWERS ) //checks to see if the pink flowers flag is set
    flags ^= PINK_FLOWERS;
//unsets the flag

I also wrote two functions to set and unset flags (again, thanks to DiCeS), and the last one is thanks to Trimbo:
int FlagIsSet( int val, int flag )
    return val & flag;
  //checks to see if the flag is set

int FlagSet( int val, int flag )
    if( FlagIsSet( val, flag ) )
  //checks to see if the flag is set
        return val | flag;
    //since flag was set, returns the val, and unsets flag
    return val;
               //since flag was not set, returns the val

int FlagUnSet( int val, int flag )
    if( FlagIsSet( val, flag ) )
  //checks to see if the flag is set
        return val ^ flag;
    //since flag was set, returns the val, and unsets flag
    return val;
               //since flag was not set, returns the val
Comments and Suggestions Follow: