Another example, maybe it'll help:
Imagine you had light that shines red, green, and blue light together.
You could map this as 3-bit binary number, {R, G, B}.
- binary 111 would mean "all components are shining".
- binary 100 would mean "only red is shining"
- binary 011 would mean "green and blue are shining"
If you had some variable that contained the state of this light, you would have the 3-bit {RGB},
where R, G, and B could either be a 0 or a 1.
RGB & 000 = 000
RGB & 001 = 00B
RGB & 010 = 0G0
RGB & 100 = R00
RGB & 011 = 0GB
RGB & 111 = RGB |
So you could check if the green light is on by doing:
1 2 3 4
|
if (RGB_value & 0b010)
{
print("green is on!")
}
|
But instead of writing 0b010 or 2, we can write it as "GreenComponent" or something of that nature, as jonnin mentioned.
1 2 3 4
|
if (RGB_value & GreenComponent)
{
print("green is on!")
}
|
1 2 3 4 5
|
int WhiteLight = RedComponent | GreenComponent | BlueComponent;
if (rgb_value & WhiteLight == WhiteLight)
{
print("All three components are on!")
}
|
_______________________
fmtflags work the same way. Each "atomic" component is a perfect power of 2, and you can combine flags together with | (bitwise OR), and test for a specific flag being enabled with & (bitwise AND).