Every computer, from a phone to a server farm, is built from a few very simple parts called logic gates. A gate takes one or two signals, each either on (1) or off (0), and gives back one signal according to a fixed rule.
That's all a gate is. The power comes from connecting billions of them. This guide covers the basic gates, and then you can wire them up yourself in our Logic Gates game.
AND: both must be on
An AND gate outputs 1 only if both inputs are 1.
Think of a car that starts only if the key is in and your foot is on the brake. One without the other does nothing.
| A | B | A AND B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
A table like this, listing every combination of inputs, is called a truth table. It's the whole definition of the gate.
OR: at least one must be on
An OR gate outputs 1 if either input is 1, or both.
A room light wired to two switches, where flipping either one turns it on, behaves like OR.
| A | B | A OR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
NOT: flip it
A NOT gate has one input and outputs the opposite: 1 becomes 0, 0 becomes 1. A "door open" warning light that's on while the door is not shut is a NOT.
The rest are combinations
Other gates are these three joined together. They get their own names because they're so useful:
- NAND ("not and") is AND followed by NOT. It outputs 0 only when both inputs are 1.
- NOR ("not or") is OR followed by NOT. It outputs 1 only when both inputs are 0.
- XOR ("exclusive or") outputs 1 when the inputs are different. Think of a staircase light with a switch at the top and bottom: flipping either one changes it.
- XNOR is the opposite of XOR: 1 when the inputs are the same.
A fun fact that surprises most people: NAND alone is enough to build any other gate, and therefore any computer. Early spacecraft computers, including the Apollo Guidance Computer, were built almost entirely from one type of NOR gate for the same reason.
From gates to adding numbers
Here's how gates become arithmetic. Add two single binary digits, A and B:
- The sum digit is A XOR B (1+0 = 1, 1+1 = 0 with a carry).
- The carry digit is A AND B (only 1+1 carries).
Those two gates together are called a half adder. Chain adders side by side and you can add numbers of any length. Everything a processor does is built up in steps like this.
Try it in the game
Logic Gates has 10 levels. Each one shows you the inputs and the output it wants, and you choose which gate goes in each slot. Level 1 is a single choice: both inputs are on and you need the output on. Later levels chain several gates and bring in NOT, XOR, NAND, NOR and XNOR. The board shows one row of inputs, but Test checks the whole truth table, so a gate that only works for that row won't pass.
Level 1: both inputs are on (1) and the target is ON. Which gate gives 1 only when all its inputs are 1?
A good way to solve the later levels: work backwards from the output. Ask "what does this last gate need to receive?", then "what does the gate before it need?", and so on to the inputs.
If you enjoy this, Robo-Link is about connecting circuits, and Turtle Code is a gentle start to writing programs, which is what all these gates end up running.