Game Development Reference
In-Depth Information
0 | 0 = 0
We will use the OR operator to ensure that the bits for a particular flag is set.
0101 (0x5)
| 1100 (0xC)
----
= 1101 (0xD)
In code we use the OR operator to create a value that holds a combination of bits, this is
what allows us to concatenate the different flags into a single value.
unsigned int value = (FLAG0 | FLAG2 | FLAG4);
// FLAG0 = 00000001
// FLAG2 = 00000100
// FLAG4 = 00010000
// value = 00010101
6.1.3 XOR Operator ^
TheXORoperatorisappliedoneachofthebitsoftwovaluesandwillreturnavaluewhich
has its bits set to 1 if one or the other value has its bits set to 1, but not both.
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
We can use the XOR operator to toggle certain bits.
0101 (0x5)
^ 1100 (0xC)
----
= 1001 (0x9)
In code we can use the XOR operation to unset a bit in a value.
unsigned int value = (FLAG0 | FLAG2 | FLAG4); // 00010101
value ^= FLAG2;
// FLAG2 = 00000100
// value = 00010001
6.1.4 Inversion Operator ~
The operator ~ is the ones complement or inversion operator, it flips the value of a bit, if
it's 1 it will become 0, if it's 0 it will become 1.
Search WWH ::

Custom Search