3 / 6 · Intro
Logic gates
Intel's first microprocessor, the 4004 of 1971, held 2,300 transistors. They were wired into gates, and gates are all a processor is made of.
Jump to the 10 practice questionsYou will learn
- Work out AND, OR, NOT, XOR, NAND and NOR
- Build a rule out of gates
Before this: Bytes, hexadecimal and text
1Explain
From Boole to Shannon
In 1854 the mathematician George Boole published An Investigation of the Laws of Thought, an algebra in which statements are either true or false and are joined with and, or and not. For decades it was a topic for logicians. In 1938 Claude Shannon, then a student at MIT, published a paper drawn from his master's thesis showing that circuits of relays and switches obey exactly that algebra: switches in series act like AND, switches in parallel like OR. Designers could now work out a circuit on paper and simplify it before building it.
2Problem
Shannon's 1938 paper
What did Shannon's 1938 paper show?
Hint
Switches in series and in parallel.
Solution
Answer: Switching circuits follow Boole's algebra of true and false
Series switches act as AND and parallel switches as OR, so circuits can be designed with algebra.
3Explain
Shannon's two circuits on a desk
You can build Shannon's two circuits with a battery, a lamp and two switches. Put the switches in a row and the lamp lights only if the first and the second are both closed. Put them side by side and the lamp lights if either one is closed. These circuits already make decisions. A logic gate is a small circuit of the same kind, built from transistors instead of hand switches.
4Problem
Two switches in a row
Two switches in series with a lamp behave like which gate?
Hint
The lamp needs the first and the second closed.
Solution
Answer: AND
Either open series switch breaks the only current path, so both must close for the lamp to light, matching AND.
5Explore
Six gates and the tables that define them
AND gives 1 only when both inputs are 1, like the switches in a row. OR gives 1 when at least one input is 1, like the switches side by side. NOT has a single input and flips it, so 0 becomes 1 and 1 becomes 0. XOR, short for "exclusive or", gives 1 when the two inputs differ. NAND and NOR are AND and OR with a NOT on the end, which makes their outputs the exact opposite. A truth table lists every combination of inputs and the output for each.
Drag to turn. Scroll or pinch to zoom.
6Problem
AND
AND with inputs 1 and 0 gives?
Hint
Both must be 1.
Solution
Answer: 0
AND outputs 1 only when every input is 1; the input 0 breaks that condition, so the output is 0.
7Problem
OR
OR with inputs 1 and 0 gives?
Hint
At least one 1.
Solution
Answer: 1
One input is 1, which is enough for OR.
8Problem
XOR
XOR with inputs 1 and 1 gives?
Hint
1 only when the inputs differ.
Solution
Answer: 0
XOR is 1 only when the inputs differ.
9Problem
NAND
NAND with inputs 1 and 1 gives?
Hint
AND, then flip.
Solution
Answer: 0
AND gives 1, and the NOT on the end flips it to 0.
10Problem
NOR
NOR with inputs 0 and 0 gives?
Hint
OR, then flip.
Solution
Answer: 1
NOR is OR followed by NOT: neither input is 1, so OR gives 0 and NOT flips it to 1.
11Explore
Gates as operators
In JavaScript & is AND, | is OR and ^ is XOR on bits. Pick A and B and compare the lines with one row of the table.
const a = 1, b = 0;
console.log("AND " + (a & b));
console.log("OR " + (a | b));
console.log("XOR " + (a ^ b));
console.log("NAND " + (1 - (a & b)));
console.log("NOR " + (1 - (a | b)));AND 0
OR 1
XOR 1
NAND 1
NOR 0| A | B | AND | OR | XOR | NAND | NOR |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 |
12Problem
Rows for three inputs
How many rows does the truth table of a 3-input gate have?
rows
Hint
Each extra input doubles the rows.
Solution
Answer: 8 rows
Each input independently has two choices, so multiply the choices for all three inputs: 2³ = 8 rows.
13Explain
An alarm, an equality test and a password check
Gates combine into rules of any size. Take a burglar alarm. It should ring when the system is armed AND (the door is open OR a window is open). That is one OR gate feeding one AND gate. To check whether two bits are equal, put a NOT after an XOR, because XOR gives 1 exactly when the bits differ. Set eight of those side by side, join their outputs with AND, and the circuit tells you whether two whole bytes match. Every comparison a program makes, from "is the password right" to "has the timer reached zero", ends up as a circuit like this.
14Explain
Test the alarm one change at a time
Test the alarm rule by changing one condition at a time. First leave the system unarmed and open the door: the final AND must keep the alarm off. Next arm the system while both openings stay closed: the OR still gives no reason to ring. Finally open the door while armed and trace the signal through both gates. These checks catch a circuit that rings for an open door even when the system is not armed.
15Explain
Why chip makers can get by with NAND alone
NAND has a handy property: every other gate can be built from NAND gates alone. Tie both inputs of a NAND together and it becomes NOT, because NAND of 1 and 1 is 0 and NAND of 0 and 0 is 1. Put that NOT after another NAND and you have AND again. A few more make OR and XOR. Chip makers like this because they can lay down enormous numbers of one well-tested shape. In a real chip a gate is a handful of transistors. The 4004 had 2,300 of them, and Intel lists 1.16 billion for its 2010 second-generation Core processor.
16Problem
A NAND with its inputs joined
You join both inputs of a NAND gate together. What gate do you get?
Hint
NAND of 1 and 1 is 0; of 0 and 0 it is 1.
Solution
Answer: NOT
NAND(0,0) = 1 and NAND(1,1) = 0: the output is the input flipped.
17Problem
The Intel 4004
How many transistors did Intel's 4004 of 1971 have?
transistors
Hint
A few thousand.
Solution
Answer: 2300 transistors
Intel’s timeline gives 2,300 transistors for the 4004; the count is of transistors, not complete logic gates.
18Practice
The burglar alarm
Finish alarm(armed, door, window): it returns 1 when the system is armed and the door or the window is open, and 0 otherwise. Use & and |.
0
0
1
1function alarm(armed, door, window) {
return 0;
}
console.log(alarm(0, 1, 0));
console.log(alarm(1, 0, 0));
console.log(alarm(1, 1, 0));
console.log(alarm(1, 0, 1));
Hint
One OR gate feeds one AND gate.
A solution
function alarm(armed, door, window) {
return armed & (door | window);
}
console.log(alarm(0, 1, 0));
console.log(alarm(1, 0, 0));
console.log(alarm(1, 1, 0));
console.log(alarm(1, 0, 1));19Explain
Common mistakes
"OR means one or the other, but not both."
Everyday "or" often works that way: "tea or coffee?" usually means pick one. In logic that rule is XOR. OR also gives 1 when both inputs are 1.
"A truth table for 3 inputs has 6 rows."
3 × 2 is the wrong count. Each extra input doubles the rows, because every earlier row appears once with the new input at 0 and once at 1. 2³ = 8. Four inputs already need 16.
"NAND and NOR are new kinds of switch."
NAND is AND with a NOT on the end, and NOR is OR with a NOT on the end. Nothing new happens inside. What makes NAND special is that it alone is enough to build every other gate.
20
Summary
- A gate turns input bits into an output bit by a fixed rule.
- AND: both 1. OR: at least one 1. NOT: flip. XOR: inputs differ.
- NAND and NOR are AND and OR with the output flipped; NAND alone can build every gate.
- A truth table with n inputs has 2ⁿ rows.