
Set Flags shows how to store multiple boolean flags in a single integer value. In programming, this is known as a bitmask or as bit flags, and the idea is based on the binary number system where each digit can be only 0 or 1 (on or off, true or false).
For example, in the binary number 1010, you have four bit flags: flags 0 and 3 are false, and flags 1 and 4 are true. The decimal equivalent of 1010 is 10 (21 + 23).
| Binary | Decimal | Power of 2 |
|---|---|---|
| 00000001 | 1 | 20 |
| 00000010 | 2 | 21 |
| 00000100 | 4 | 22 |
| 00001000 | 8 | 23 |
| 00010000 | 16 | 24 |
| 00100000 | 32 | 25 |
| 01000000 | 64 | 26 |
| 10000000 | 128 | 27 |