Deep inside every processor, decisions are made not with words but with tiny switches that answer one question: given these inputs (each a 1 or a 0), what should the output be? Wire those switches together and you get logic gates — the building blocks of every calculation a computer performs. Master six of them and you can read the electronic reasoning behind any chip.
The six gates and their truth tables
Each gate takes one or two inputs (A, B) and gives one output. "1" means true/on, "0" means false/off.
| A | B | AND | OR | NAND | NOR | XOR |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 |
- AND — output 1 only when both inputs are 1.
- OR — output 1 when at least one input is 1.
- NOT — one input; it inverts it (0→1, 1→0).
- NAND — NOT AND: the opposite of AND.
- NOR — NOT OR: the opposite of OR.
- XOR — exclusive OR: output 1 only when the inputs differ.
Building and reading a circuit
To find the output of a circuit, work left to right: evaluate the first gate, feed its result into the next, and so on. To build a circuit from a statement such as "the alarm sounds if the door is open AND either the key is off OR the window is open", you turn each word into a gate.
Completing a truth table
For the circuit X = (A AND B) OR C, list every combination of A, B, C (there are 2³ = 8 rows), work out A AND B, then OR that with C.
| A | B | C | A AND B | X = (A AND B) OR C |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 0 | 0 |
| 1 | 1 | 0 | 1 | 1 |
| 0 | 0 | 1 | 0 | 1 |
A AND B = 1 AND 0 = 0. Then X = 0 OR C = 0 OR 0 = 0. So X = 0.
Now A=1, B=1, C=0: A AND B = 1; X = 1 OR 0 = 1.
Memory hook: AND = All must be 1; OR = One or more; XOR = eXclusively different. A circle on the output means "flip it".