A scooter's engine starts only if the key is turned and the brake is pressed. A doorbell rings if the front button or the back button is pressed. A night lamp turns on when it is not light outside. You've just met the three basic logic gates: AND, OR and NOT. Every calculation a computer makes, from adding marks to playing a video, comes down to millions of these simple decisions on 0s and 1s.
A logic gate is a small circuit, built from a few transistors, that takes one or more bits in and gives one bit out by a fixed rule. This lesson covers the seven standard gates, their truth tables, how to combine them, and the surprising fact that one gate, NAND, is enough to build an entire computer. You'll simulate every gate in Python, and if you've written and, or and not in an if statement, you already know half of this.
What you need first: transistors: how a computer understands binary.
Key terms
- Logic gate: a circuit that takes input bits and produces an output bit by a fixed rule.
- Truth table: a table listing every possible input and the output for each.
- Input / output: the bits going in (usually called A and B) and the bit coming out (often called Y or Q).
- Boolean: having only two values, true or false (1 or 0). Named after the mathematician George Boole.
- Universal gate: a gate from which every other gate can be built. NAND and NOR are universal.
- Combinational circuit: a circuit whose output depends only on its current inputs, with no memory.
The three basic gates
AND: output 1 only if all inputs are 1
The scooter starts only when the key is on and the brake is pressed.
| A (key) | B (brake) | A AND B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Written as A · B or AB in Boolean algebra.
OR: output 1 if at least one input is 1
The doorbell rings if the front or the back button is pressed, or both.
| A (front) | B (back) | A OR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Written as A + B. (It's not addition: in Boolean algebra, 1 + 1 = 1.)
NOT: flips the input
The night lamp is on when it's not light. NOT has only one input.
| A (light outside) | NOT A (lamp) |
|---|---|
| 0 | 1 |
| 1 | 0 |
Written as A', Ā or ¬A. NOT is also called an inverter.
The other four gates
| Gate | Rule | Output is 1 when | Written as |
|---|---|---|---|
| NAND | NOT AND | Not all inputs are 1 | (AB)' |
| NOR | NOT OR | All inputs are 0 | (A + B)' |
| XOR (exclusive OR) | Different inputs | Exactly one input is 1 | A ⊕ B |
| XNOR | NOT XOR | Inputs are the same | (A ⊕ B)' |
All seven together, for every pair of inputs:
| A | B | AND | OR | NAND | NOR | XOR | XNOR |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 | 1 |
Real-life XOR: a staircase light with a switch at the bottom and the top. Flipping either switch changes the light, because the light is on when the switches are in different positions.
In circuit diagrams, each gate has its own symbol: AND is a "D" shape, OR is a curved shield shape, NOT is a triangle with a small circle at its tip, and the small circle (a "bubble") on any gate's output means NOT, so NAND is the AND shape with a bubble.
Logic gates in Python
Python's & (and), | (or) and ^ (xor) work on bits directly. Here's each gate as a function, with a truth table printed for every one:
from itertools import product
def AND(a, b): return a & b
def OR(a, b): return a | b
def NOT(a): return 1 - a
def NAND(a, b): return NOT(AND(a, b))
def NOR(a, b): return NOT(OR(a, b))
def XOR(a, b): return a ^ b
def XNOR(a, b): return NOT(XOR(a, b))
gates = [AND, OR, NAND, NOR, XOR, XNOR]
print("A B " + " ".join(f"{g.__name__:>4}" for g in gates))
for a, b in product([0, 1], repeat=2):
print(f"{a} {b} " + " ".join(f"{g(a, b):>4}" for g in gates))
A B AND OR NAND NOR XOR XNOR
0 0 0 0 1 1 0 1
0 1 0 1 1 0 1 0
1 0 0 1 1 0 1 0
1 1 1 1 0 0 0 1
product([0, 1], repeat=2) gives every combination of two inputs: (0, 0), (0, 1), (1, 0), (1, 1). With 3 inputs there are 2³ = 8 rows, and with n inputs, 2ⁿ.
These are the same ideas as Python's and, or and not keywords in an if statement. The difference is that if works on true/false conditions, while gates work on single bits; underneath, the CPU runs your if using gates.
Combining gates
Gates connect output to input to make bigger circuits. Say a lift should move only if the door is closed (D = 1) and either a floor button is pressed (F = 1) or someone called it from outside (C = 1):
Move = D AND (F OR C)
from itertools import product
print("D F C Move")
for d, f, c in product([0, 1], repeat=3):
move = d & (f | c)
print(d, f, c, " ", move)
D F C Move
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 1
The truth table shows the lift never moves with the door open, whatever buttons are pressed. Writing out the full table is the most reliable way to check a circuit does what you meant.
Multi-input gates work the same way: a 3-input AND outputs 1 only if all three inputs are 1; a 3-input OR outputs 1 if any input is 1.
NAND can build everything
Here's a remarkable fact: you can build every gate from NAND gates alone. That's why NAND is called a universal gate. NOR is universal too.
| Gate | Built from NAND |
|---|---|
| NOT A | NAND(A, A) |
| A AND B | NOT(NAND(A, B)), which is NAND(NAND(A, B), NAND(A, B)) |
| A OR B | NAND(NOT A, NOT B) |
Check it in Python, using only NAND:
from itertools import product
def NAND(a, b):
return 0 if (a == 1 and b == 1) else 1
def NOT(a): return NAND(a, a)
def AND(a, b): return NOT(NAND(a, b))
def OR(a, b): return NAND(NOT(a), NOT(b))
def XOR(a, b): return AND(OR(a, b), NAND(a, b))
for a, b in product([0, 1], repeat=2):
assert NOT(a) == 1 - a
assert AND(a, b) == (a & b)
assert OR(a, b) == (a | b)
assert XOR(a, b) == (a ^ b)
print("Every gate built from NAND matches")
Every gate built from NAND matches
The assert lines stop the program with an error if any result is wrong; reaching the last line means all 16 checks passed. Chip makers rely on this: a factory that can make NAND gates very well can make anything. (NAND flash memory in SSDs is named after the same gate, because its cells are wired in a similar pattern.)
Where gates are used
- Adders: XOR and AND gates add binary numbers; see half adder and full adder.
- Multiplexers: AND, OR and NOT gates choose between inputs; see multiplexers and decoders.
- Memory: two NOR or NAND gates wired in a loop can remember one bit, as from logic gates to a CPU shows.
- Comparison: XNOR outputs 1 when two bits are equal, so a row of XNORs checks whether two numbers are the same.
- Parity and checksums: XOR of all the bits of a byte tells you whether it has an odd number of 1s, which helps detect transmission errors.
- Encryption: XOR with a secret key scrambles data, and XOR with the same key again unscrambles it.
Practice
- Fill in the output of
A NOR Bfor A = 0, B = 0 and for A = 1, B = 0.
Show the answer
NOR is 1 only when all inputs are 0. So A = 0, B = 0 gives 1, and A = 1, B = 0 gives 0.
- A bank locker opens only if the manager's key and the customer's key are both turned. Which gate is this? What if either key alone should open it?
Show the answer
Both keys needed: AND. Either key: OR.
- Use Python to print the truth table of the 3-input circuit
Y = (A AND B) OR (NOT C). How many rows output 1?
Show the answer
from itertools import product
count = 0
for a, b, c in product([0, 1], repeat=3):
y = (a & b) | (1 - c)
count += y
print(a, b, c, y)
print("Rows with 1:", count)
0 0 0 1
0 0 1 0
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 1
1 1 1 1
Rows with 1: 5
Every row with C = 0 outputs 1 (four rows), plus the row A = 1, B = 1, C = 1.
- Use XOR to work out whether the byte
10110100has an odd or even number of 1s.
Show the answer
bits = [1, 0, 1, 1, 0, 1, 0, 0]
parity = 0
for bit in bits:
parity ^= bit
print(parity)
0
The result is 0, so there's an even number of 1s (four). A result of 1 would mean odd.
- Build NOR from NAND gates only, and check it in Python.
Show the answer
NOR is NOT OR, and OR is NAND(NOT A, NOT B), so NOR = NOT(NAND(NOT A, NOT B)).
from itertools import product
def NAND(a, b): return 0 if (a and b) else 1
def NOT(a): return NAND(a, a)
def NOR(a, b): return NOT(NAND(NOT(a), NOT(b)))
print([NOR(a, b) for a, b in product([0, 1], repeat=2)])
[1, 0, 0, 0]
Interview questions
What are universal gates, and why are they important?
NAND and NOR are universal gates: any Boolean function, and so any digital circuit, can be built using only one of them. This simplifies chip manufacturing and design, since one well-optimised gate type can implement everything.
What is the difference between OR and XOR?
OR outputs 1 when at least one input is 1, including when both are. XOR outputs 1 only when exactly one input is 1 (the inputs differ), so for inputs 1 and 1, OR gives 1 and XOR gives 0.
Key takeaways
- A logic gate takes input bits and gives one output bit by a fixed rule; a truth table lists every case.
- AND needs all inputs 1, OR needs at least one, NOT flips its input.
- NAND, NOR and XNOR are NOT versions of AND, OR and XOR; XOR is 1 when the inputs differ.
- n inputs give 2ⁿ rows in a truth table; writing it out is the surest way to check a circuit.
- NAND (and NOR) are universal: every circuit, even a whole CPU, can be built from them alone.
Frequently asked questions
How many transistors are in a logic gate?
In CMOS, a NOT gate uses 2 transistors, NAND and NOR use 4, and AND and OR use 6 (a NAND or NOR followed by a NOT). That's one reason chip designers prefer NAND and NOR.
Are logic gates the same as Python's and, or and not?
They follow the same logic. Python's &, | and ^ work bit by bit like gates, while and, or and not work on whole true/false values and are used in conditions. When your program runs, the CPU carries out these operations with real gates.
What is a truth table used for?
It lists the output for every possible combination of inputs. Designers use it to describe what a circuit must do, and then to check that a design or a simplified formula gives exactly the same outputs.
What to learn next
- Simplify circuits with Boolean algebra and Karnaugh maps.
- Use gates to add numbers in half adder and full adder.
- Revise how gates are made in transistors: how a computer understands binary.