Last active
November 30, 2015 20:26
-
-
Save antonshkurenko/926082e22e1c5a71e339 to your computer and use it in GitHub Desktop.
Bit flags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is an example, using a 8-bit unsigned integer to store 8 flags: | |
`unsigned char options;` | |
It is common to use larger fields, e.g. 32 bits, but I use 8 here for simplicity. | |
The possible options, that can be turned on or off independently are declared in an enum like this (just using some arbitrary identifiers): | |
``` | |
enum Options { | |
OpAutoRedraw = 0x01, | |
OpAntiAlias = 0x02, | |
OpPixelShader = 0x04, | |
OpVertexShader = 0x08, | |
OpFullscreen = 0x10, | |
OpDaylight = 0x20 | |
// ... | |
}; | |
``` | |
Note how each option is given a specific value. These values are carefully picked to match each bit in the 8-bit variable: | |
``` | |
// 0x01 == 1 == "00000001" | |
// 0x02 == 2 == "00000010" | |
// 0x04 == 4 == "00000100" | |
// 0x08 == 8 == "00001000" | |
// 0x10 == 16 == "00010000" | |
// 0x20 == 32 == "00100000" | |
// 0x40 == 64 == "01000000" | |
// 0x80 == 128 == "10000000" | |
``` | |
Now, each flag can be set independently, by using the bitwise OR operator: | |
``` | |
options = OpAutoRedraw | OpVertexShader | OpFullscreen; | |
// options == 0x01 | 0x08 | 0x10 == "00011001" | |
``` | |
And can be tested using the bitwise AND operator: | |
``` | |
if (options & OpAutoRedraw) {} // true | |
if (options & OpAntiAlias) {} // false | |
``` | |
I hope this helps! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment