Digital Logic
Logic gates explained
A logic gate is a circuit that makes a decision from bits, signals that are either 1 or 0. It takes one or more inputs and gives one output by a fixed rule. AND gives 1 only when every input is 1, OR gives 1 when any input is 1, and NOT flips its input. Every digital chip, from a calculator to a processor, is built from gates like these.
The rule is a truth table
A gate's rule is its truth table: one row for every combination of inputs, with the output for each. With n inputs there are 2ⁿ rows, so a two-input gate has four.
A gate is a lot like switches in a loop. AND is two switches in series: both must close to light the lamp. OR is two switches in parallel: either one will do. The difference is that a gate makes a fresh, full-strength 0 or 1 at its output, so thousands can follow one another without the signal fading.
Live experiment
Meet the gate family
Pick AND, OR or XOR and flip the two input switches. The four lamps under the gate show its whole truth table, one per input setting. Then add the bubble to make NAND, NOR or XNOR.
“Logic gates” is lesson 2 of 6 in Logic Grid, Electronics World 7. Free with an account. Lessons unlock in order, so you’ll do the 43 before it first.
The whole family
| A | B | AND | OR | XOR | NAND | NOR | XNOR |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 | 0 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 | 1 |
XOR, exclusive OR, gives 1 when its inputs differ. A small circle on a gate's output, the bubble, is a NOT: it flips every answer. AND with a bubble is NAND, OR with one is NOR, and XOR with one is XNOR, which gives 1 when its inputs match.
Writing gates down
Boolean algebra has a shorthand. AND is written A · B, OR is A + B, NOT is Ā (said "not A"), and XOR is A ⊕ B. The + means OR, not adding, so in logic 1 + 1 = 1.
A sentence turns straight into gates. A garden light should come on when it's dark and something moves, or when you flip its switch:
The brackets say which gate comes first: an AND for DARK · MOTION, then an OR with SWITCH. Move the brackets and the rule changes.
Where you'll meet them
A car's courtesy light is an OR of its doors, and a two-hand safety press is an AND, so neither hand can be near the blade. Stair lights you can switch from either end follow XOR. Inside a chip, each gate is a handful of transistors, and each takes a moment to respond, its propagation delay: a few nanoseconds in a 74HC chip, a few picoseconds inside a modern processor. A clock can only tick as fast as its slowest chain of gates.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Logic gates” is lesson 2 of 6 in Logic Grid, Electronics World 7. Free with an account. Lessons unlock in order, so you’ll do the 43 before it first.
Or start from the very first lesson, no account neededWork it out
Practice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: either is closed. In parallel, the current has two paths, so either closed switch completes the loop. That's an OR gate made of switches. In series, both must close, and that's AND.
- Why not “both are closed”? Both closed lights it too, but it isn't needed. Needing both to close is what two switches in series do: an AND gate.
- Why not “neither is closed”? With neither closed, both paths are open and no current can flow, so the lamp stays dark.
Show the answer
Answer: NAND. NAND is universal. Tied inputs make a NOT, a NOT after a NAND makes an AND, and with De Morgan three NANDs make an OR.
- Why not “NOT”? NOT has only one input, so it can flip a signal but never combine two. You can't build AND or OR from NOTs alone.
- Why not “XOR”? XOR gates on their own only ever make other XOR-like functions. No arrangement of them gives AND, so XOR isn't universal.
Show the answer
Answer: A and B are both 1. A · B is 1 only when both inputs are 1, so NOT(A · B) is 0 only then. By De Morgan it equals Ā + B̄, which is 1 whenever either input is 0.
- Why not “A and B are both 0”? With both inputs at 0, A AND B is 0, so NOT(A AND B) is 1, not 0.
- Why not “either input is 1”? If only one input is 1, A AND B is 0, so NOT(A AND B) is 1. It takes both inputs at 1 to give 0.
“Logic gates” is lesson 2 of 6 in Logic Grid, Electronics World 7. Free with an account. Lessons unlock in order, so you’ll do the 43 before it first.
Questions people ask
What are the basic logic gates?
AND gives 1 only when all its inputs are 1. OR gives 1 when any input is 1. NOT flips a single input. NAND, NOR and XNOR are AND, OR and XOR followed by a NOT. XOR gives 1 when its two inputs differ.
What is the difference between OR and XOR?
They differ only when both inputs are 1. OR gives 1 for 1 and 1, because at least one input is 1. XOR, exclusive OR, gives 0 there: it means one or the other, but not both.
How are logic gates made?
From transistors. In CMOS, the kind in almost every modern chip, a NAND gate is four MOSFETs: two in series that pull the output to 0, and two in parallel that pull it to 1.
Why is NAND called a universal gate?
Every other gate can be built from NAND gates alone. A NAND with its inputs tied together is a NOT, a NAND followed by a NOT is an AND, and so on. NOR is universal in the same way.
Related topics
- Binary numbers explainedPlaces worth 1, 2, 4 and 8: add up the bits that are on, and four wires count to 15.
- Transistor as a switch, explainedA trickle into the base switches a big current: cutoff, saturation and the base resistor.
- Op-amp comparator explainedTwo inputs, one decision: the output slams high or low the moment one input passes the other.