Level 0: How computers work · 12 min
Logic gates: switches that decide
Goal: know what the AND, OR, NOT and XOR gates do.
You need: Binary.
The problem
We can now hold numbers as bits. But a computer must also do things with them, like compare and add.
One switch cannot decide anything. But several switches connected together can follow a rule.
What is a logic gate?
A logic gate is a small circuit that takes one or two bits as input and gives one bit as output. It always follows the same rule. It is built from transistors.
Two switches, two rules
Put two switches one after the other on the same path. Current gets through only if the first and the second are closed.
Put them side by side, on two separate paths to the bulb. Current gets through if the first or the second is closed.
These are the ideas behind the first two gates.
AND: both must be yes
A cash machine gives money only if your card is in and your PIN is correct.
| Card in? | PIN correct? | Money? |
|---|---|---|
| no (0) | no (0) | no (0) |
| no (0) | yes (1) | no (0) |
| yes (1) | no (0) | no (0) |
| yes (1) | yes (1) | yes (1) |
AND gives 1 only when both inputs are 1.
OR: at least one is yes
A house with two doorbell buttons. The bell rings if someone presses the front button or the back button.
| Front pressed? | Back pressed? | Rings? |
|---|---|---|
| no (0) | no (0) | no (0) |
| no (0) | yes (1) | yes (1) |
| yes (1) | no (0) | yes (1) |
| yes (1) | yes (1) | yes (1) |
OR gives 1 when at least one input is 1.
NOT: the opposite
A fridge light is on when the door is not closed.
| Door closed? | Light on? |
|---|---|
| yes (1) | no (0) |
| no (0) | yes (1) |
NOT has only one input. It flips the bit.
XOR: one, but not both
A staircase lamp often has two switches, one downstairs and one upstairs. Start with both down and the lamp off. Flip either one and the lamp changes. Here 1 means "this switch is flipped up".
| Downstairs | Upstairs | Lamp on? |
|---|---|---|
| 0 | 0 | no (0) |
| 0 | 1 | yes (1) |
| 1 | 0 | yes (1) |
| 1 | 1 | no (0) |
XOR gives 1 when the two inputs are different. The name is short for "exclusive OR": one or the other, but not both.
Try it
Choose a gate. Before you change an input, say what the output will be. Then check.
1 only when both inputs are 1.
Output 0
| A | B | AND |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Truth tables
The table under the demo is a truth table. The small tables in the examples above were truth tables too. It lists every possible input and the output for each one. Two inputs have only 4 combinations, so we can check them all.
| A | B | AND | OR | XOR |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
What comes next
Look at the AND and XOR columns again. In the next lesson, these two gates work together to add numbers.
Check yourself
Practice
An alarm must ring when the system is armed and at least one of two things happens: the door opens or the window opens. Which two gates do you need, and how do you connect them?
Show answer
First put door and window into an OR gate. Then put that result and "armed" into an AND gate. alarm = armed AND (door OR window)
Recap
- A logic gate takes bits in and gives one bit out, using a fixed rule.
- AND: both. OR: at least one. NOT: the opposite. XOR: different.
- A truth table lists every input and its output.
- Gates connected together can do arithmetic.