A binary flag is a binary number that is used to indicate that a single switch in a binary-word is on or off; Along with another binary number called a binary mask the two can have a binary AND operation applied to it to determine if the flag is on or off.
Querying the Status of a bit:
It is possible to use bitmasks to easily check the state of individual bits regardless of the other bits. To do this, turning off all the other bits using the bitwise AND is done as discussed above and the value is compared with 0. If it is equal to 0, then the bit was off, but if the value is any other value, then the bit was on. What makes this convenient is that it is not necessary to figure out what the value actually is, just that it is not 0.
Example 1 Example 2
10011101 10010101 <- Binary Flags
AND 00001000 00001000 <- Bit Mask
= 00001000 00000000 <- Bit Status (on or off)