AND, OR, XOR and NOT
A settings screen has eight on/off switches. You could store eight separate true/false values, or you could store one byte where each bit is a switch. To read and change those switches you need operators that work on individual bits. Every mainstream language has them, usually written &, |, ^ and ~.
The rule
AND needs both, OR needs either, XOR needs exactly one, NOT flips.
Line the two numbers up in binary and work column by column, with no carrying between columns:
| a | b | a AND b | a OR b | a XOR b |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
NOT takes one number and flips every bit: in 8 bits, NOT 00001111 is 11110000. (In languages with signed integers, ~x equals −x − 1, because of how negative numbers are stored; Python's ~5 is −6.)
12 & 10 # AND -> 8
12 | 10 # OR -> 14
12 ^ 10 # XOR -> 6Do not confuse these with the logical operators and, or, &&, ||, which treat a whole value as true or false. Bitwise operators look inside the number.