Game Development Reference
In-Depth Information
FLAG0 = 00000001
FLAG1 = 00000010
FLAG2 = 00000100
FLAG3 = 00001000
FLAG4 = 00010000
FLAG5 = 00100000
This is precisely what we need because it will allow us to combine flags as needed, first
let's do a quick review of the bitwise operations.
6.1.1 AND Operator &
The AND operator is applied on each of the bits of two values and will return a value that
has its bits set to 1 if and only if both values have their corresponding bits set.
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
We will use the AND operator to verify if one or several flags we are testing for are en-
abled.
0101 (0x5)
& 1100 (0xC)
----
= 0100 (0x4)
In code we use the AND operator to verify if a particular flag or combination of flags are
set within a value.
unsigned int value = (FLAG0 | FLAG2 | FLAG4);
if ( value & FLAG4 == FLAG4 )
{
// bit at position 4 is set.
}
6.1.2 OR Operator |
The OR operator is applied on each of the bits of two values and will return a value which
has its bits set to 1 if one or both of the bits are set.
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
Search WWH ::




Custom Search