Logic Gates & Truth Tables
Every decision a computer makes — from adding numbers to showing this page — is built from tiny switches that answer yes or no.
Swipe or tap Next for the next section
Yes or No, 1 or 0
A computer's chips contain billions of tiny electronic switches. Each one is either on or off — 1 or 0. A logic gate takes one or two of those 1s and 0s as inputs and gives a single 1 or 0 back out.
There are only a few kinds, and you already use them in everyday sentences:
- AND — "You can go out if your homework is done AND your room is tidy." Both must be true.
- OR — "We'll cancel if it rains OR it snows." At least one must be true.
- NOT — "If it's NOT raining, we'll walk." It flips the answer.
A logic gate turns inputs of 1 and 0 into a single output of 1 or 0, following one simple rule.
Truth Tables
A truth table lists every possible input and the output you get. With two inputs there are four rows, because each input can be 0 or 1.
| A | B | Out |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
| A | B | Out |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
| A | Out |
|---|---|
| 0 | 1 |
| 1 | 0 |
A quick way to remember: AND is only 1 when everything is 1. OR is only 0 when everything is 0.
The Same Logic, in Code
Programs use exactly the same three words to make decisions. In pseudocode they combine conditions inside an IF:
IF (age ≥ 13 AND hasPermission)
{
DISPLAY("Account created")
}
IF (NOT loggedIn)
{
DISPLAY("Please log in")
}Each condition is either true or false — a 1 or a 0 — and AND, OR and NOT combine them just like the gates above. A long condition is worked out from the inside out, the same way brackets work in math.
From Gates to Computers
One gate can't do much. But wire a few together and you can add two bits. Wire thousands together and you can add big numbers, compare them, and remember them. A modern processor contains billions of gates — and every one is still just answering "1 or 0?"
That's another example of abstraction: nobody designs a phone one gate at a time. Engineers build small parts from gates, bigger parts from those, and so on up.
5 quick questions. Pick one answer for each — you'll see why straight away.
A door alarm should ring only when the alarm is switched on AND the door is open. Which gate is that?
Both conditions have to be true at the same time, which is exactly what AND checks.
What does A OR B give when A is 1 and B is 1?
OR is 1 when at least one input is 1 — and both being 1 still counts. Logic gates only ever output 0 or 1.
How many rows does the truth table for a gate with 2 inputs have?
Each input can be 0 or 1, so there are 2 × 2 = 4 combinations: 00, 01, 10, 11. Three inputs would need 8.
In a program, IF (NOT raining) { DISPLAY("Go outside") }. When is "Go outside" displayed?
NOT flips the value, so NOT raining is true exactly when raining is false.
Which output does A AND (NOT B) give when A = 1 and B = 1?
NOT B is 0, and 1 AND 0 is 0. Work from the inside out, just like brackets in math.