Logic gates: how switches make decisions
AND, OR and NOT are tiny decision makers built from switches. Combine them and a computer can even add numbers.
A computer is built from switches. But switches on their own just sit there. How do they decide things, like "is this number bigger?" or "add these two numbers"?
The answer is to wire switches together in clever patterns. These patterns are called logic gates.
A switch you can flip with electricity
A normal switch is flipped by your finger. A transistor is a switch that is flipped by electricity instead. That means the output of one transistor can flip another. Chain them together, and switches can control other switches. That is how decisions are made.
Two circuits you already know
You met these in Circuit Rescue. They are the heart of logic.
Two switches in a row (series). The lamp lights only if the first switch and the second switch are closed. This is called AND.
Two switches side by side (parallel). The lamp lights if the first switch or the second one is closed. This is called OR.
There is a third basic gate, NOT. It flips the answer: if the input is 1, the output is 0, and if the input is 0, the output is 1.
Truth tables
We can list what a gate does for every possible input. This is called a truth table. A 1 means on and a 0 means off.
AND: output is 1 only if both inputs are 1.
| A | B | A AND B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
OR: output is 1 if at least one input is 1.
| A | B | A OR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Play with the gates
Choose a gate, flip the inputs, and see the lamp. Watch the row of the table that matches.
AND: the output is 1 only when both inputs are 1.
| A | B | Output |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Try XOR, which stands for "exclusive or". It is 1 when the inputs are different. And NAND, which is the opposite of AND. (A fun fact: using only NAND gates, you can build every other gate.)
Gates can do maths
Here is the amazing part. Gates do not just decide. They can add.
Add two single bits:
| A | B | Sum bit | Carry bit |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
In the last row, 1 + 1 = 2, which in binary is 10. That means sum bit 0 and carry bit 1, just like carrying a 1 when you add 9 + 1 in everyday arithmetic.
Look closely at the table:
- The sum bit is 1 when the inputs are different. That is XOR.
- The carry bit is 1 when both are 1. That is AND.
So one XOR gate and one AND gate make a tiny adder! Chain adders together, and you can add numbers of any size. Add more gate patterns, and you can subtract, multiply and compare.
All the way up
A modern processor has billions of transistors, arranged as gates, arranged as adders and other circuits, arranged into bigger parts. Everything a computer ever does, from a video call to a game, comes down to switches turning on and off in the right order.
In Circuit Rescue, the series and parallel circuits are exactly AND and OR. A computer chip is the same idea, shrunk to a size you need a microscope to see, and repeated billions of times.
Words to know
- Logic gate
- A small circuit that takes one or two input bits and gives one output bit by a fixed rule.
- AND
- Output is 1 only when both inputs are 1.
- OR
- Output is 1 when at least one input is 1.
- NOT
- Flips the input: 1 becomes 0, and 0 becomes 1.
- Truth table
- A list of what a gate does for every possible input.
- Adder
- A circuit made of gates that adds binary numbers.
Check yourself
What is the output of an AND gate if the inputs are 1 and 0?
Show the answer
0. AND needs both inputs to be 1.
What is the output of an OR gate if the inputs are 1 and 0?
Show the answer
1. OR needs only one input to be 1.
Which two gates make a tiny adder, and what do they give?
Show the answer
XOR gives the sum bit and AND gives the carry bit.
What kind of circuit is an AND gate, in switch language?
Show the answer
Two switches in a row (series). Both must be closed.