Logic Gates
Every digital circuit is built from a handful of gates. This volume meets all seven: what each one does, how it is drawn and how it is written. Then it shows how to work out what a circuit of several gates does, why real gates take time, why NAND and NOR can build everything, and what gates look like on a real chip.
- The symbols, formulas and truth tables of AND, OR, NOT, NAND, NOR, XOR and XNOR
- How to find the truth table of a circuit, column by column
- What propagation delay is, and how it causes glitches
- Why NAND and NOR are universal, and how many of each every gate needs
- How 7400-series chips are laid out and wired
- Volumes 00 and 01 of this course: bits, truth tables and binary numbers.
2.1 AND, OR and NOT
Three gates can build any logic circuit there is. An AND gate gives 1 only when all its inputs are 1. An OR gate gives 1 when at least one input is 1. A NOT gate turns 0 into 1 and 1 into 0.
Volume 00 found AND and OR hiding in two switches and a lamp. This volume gives each gate its symbol, its truth table and a short way to write it down. Figure 2.1 shows the first three.
AND
The output of an AND gate is 1 only when every input is 1. We write it with a dot, Y = A·B, and read it as "Y equals A and B".
| A | B | Y |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
A car that starts only when the key is turned and the brake is pressed is following an AND rule.
OR
The output of an OR gate is 1 when at least one input is 1. We write it with a plus sign, Y = A + B, and read it as "Y equals A or B".
| A | B | Y |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
A car's courtesy light that comes on when either front door is open is following an OR rule.
Reading + as adding. In logic, + means OR, so 1 + 1 is not 2. Both inputs are 1, so at least one is 1, and the answer is 1:
in logic, 1 + 1 = 1
NOT
A NOT gate has one input, and its output is always the opposite. It is also called an inverter. We write it with a small mark after the letter, Y = A', and read it as "Y equals not A". Many books draw a bar over the letter instead; both mean the same.
| A | Y |
|---|---|
| 0 | 1 |
| 1 | 0 |
The small circle on the front of the NOT symbol is called a bubble. Wherever you see one on a gate, it means the signal is inverted at that point.
More than two inputs
AND and OR gates can have more inputs, and the rules stay the same. A three-input AND needs all three inputs to be 1, while a three-input OR needs just one:
| A | B | C | AND | OR |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
AND is 1 on just the last row. OR is 0 on just the first.
A three-input OR gate has inputs 0, 0 and 1. What is its output?
Show the answer
Answer: B. An OR gate gives 1 when at least one input is 1. One input is 1, so the output is 1. The order of the inputs never matters for AND or OR.
2.2 NAND, NOR, XOR and XNOR
Four more gates complete the set. NAND and NOR are AND and OR with the output inverted. XOR gives 1 when its inputs differ, and XNOR gives 1 when they are the same.
Here are all four in one truth table:
| A | B | NAND | NOR | XOR | XNOR |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 0 | 1 |
| 0 | 1 | 1 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 0 | 1 |
NAND and NOR
NAND means "not AND". Its column is the AND column with every bit flipped, so it gives 0 only when all its inputs are 1. We write it Y = (A·B)', with the bracket showing that the whole AND is inverted.
NOR means "not OR". It gives 1 only when all its inputs are 0, and we write it Y = (A + B)'.
These two look like afterthoughts, but they are the most important gates of all. Sub-module 2.4 shows why.
XOR and XNOR
XOR, short for exclusive OR, gives 1 when its inputs are different. It is like OR, except that it excludes the row where both inputs are 1. We write it Y = A ⊕ B.
XNOR is XOR inverted: it gives 1 when the inputs are the same. We write it Y = A ⊙ B. You have met it before. It is exactly the staircase light from Volume 00, where the lamp lit when both switches pointed the same way:
XNOR is the staircase rule of Volume 00: True
It is also the "same or different" rule that turned binary into Gray code in Volume 01. That rule was XOR all along.
XOR with more inputs
A three-input XOR gives 1 when an odd number of its inputs are 1. That makes it the gate that calculates a parity bit:
| A | B | C | A ⊕ B ⊕ C |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 1 |
XOR as a switchable inverter
Look at the XOR rows again. When B is 0, the output equals A. When B is 1, the output is A inverted. So one input of an XOR can switch inversion on and off for the other. Applied to four bits at once:
1011 XOR 0000 = 1011
1011 XOR 1111 = 0100
1011 XOR 0110 = 1101
A 0 in the second number leaves a bit alone, and a 1 flips it. Volume 06 uses exactly this to build a circuit that can both add and subtract.
Which gate gives 1 only when both of its two inputs are 0?
Show the answer
Answer: C. NOR is OR inverted. OR gives 0 only when both inputs are 0, so NOR gives 1 only then. XNOR is also 1 when both inputs are 0, but it is 1 when both are 1 as well.
2.3 Truth tables and timing diagrams
A truth table and a timing diagram show the same facts in two ways. A truth table lists every input pattern once. A timing diagram shows the patterns in the order they happen - and it can show things a truth table cannot, because real gates take time.
Building a truth table, column by column
To find the truth table of a circuit with several gates, add a column for each gate's output and work from the inputs towards the output. Take the circuit in Figure 2.3, which ANDs A and B, then ORs the result with C.
| A | B | C | A·B | Y |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 0 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 1 |
First fill the A·B column, which is 1 only where A and B are both 1. Then OR it with C: Y is 1 wherever A·B is 1 or C is 1.
Write the rows in counting order: 000, 001, 010 and so on up to 111. Then no row is ever missed or repeated, and anyone can check your table against theirs row by row.
The same circuit over time
Figure 2.4 shows the same circuit while its inputs change. Each column of the timing diagram is one row of the truth table, in the order it happened.
Gates take time
So far every output has changed at the same instant as its inputs. Real gates are not that quick. A gate's output changes a short time after its input does, and that time is the gate's propagation delay.
Figure 2.5 shows a NOT gate whose delay is one time step. Every change of the output comes one step after the change of the input that caused it:
A changes at 3, 6 and 9; Y changes at 4, 7, 10
A glitch
The delay has a surprising result. On paper, A·A' is always 0, because A and A' can never both be 1:
| A | A' | A·A' |
|---|---|---|
| 0 | 1 | 0 |
| 1 | 0 | 0 |
Now build it: A goes straight into an AND gate, and also through a NOT gate into the AND's other input. When A rises, the NOT gate takes a step to catch up. For that one step, both AND inputs are 1:
That short, unwanted pulse is a glitch. The truth table is still right about where the output settles. The timing diagram shows what happens on the way there. Volume 04 explains when glitches happen and how to design them out.
A gate has a propagation delay of 8 ns. Its input changes at 100 ns. When does its output change?
Show the answer
Answer: A. The output changes one propagation delay after the input: 100 + 8 = 108 ns. It can never change before the input does.
2.4 Universal gates: everything from NAND
NAND on its own can build every other gate, and so can NOR. That is why they are called universal gates, and why real chips are built mostly from them.
NOT, AND and OR from NAND
A NAND with both inputs joined together is a NOT gate, because (A·A)' is just A'. A NAND followed by that NOT undoes the inversion and gives AND. And inverting both inputs of a NAND gives OR, as Figure 2.7 shows.
Why does the bottom circuit give OR? A NAND gives 1 unless both its inputs are 1. Its inputs here are A' and B', which are both 1 only when A and B are both 0. So the output is 1 unless A and B are both 0 - and that is exactly OR. Volume 03 turns this reasoning into a rule, De Morgan's theorem.
XOR from four NANDs
Even XOR can be built from NANDs. Figure 2.8 uses four, and the simulator behind this page checked all four input rows.
How many gates each one takes
How many NAND gates does each gate need at the very least? A program tried every possible network of one NAND, then two, then three, and so on, until it found each gate. It did the same for NOR:
| Gate | NAND gates | NOR gates |
|---|---|---|
| NOT A | 1 | 1 |
| AND | 2 | 3 |
| OR | 3 | 2 |
| NAND | 1 | 4 |
| NOR | 4 | 1 |
| XOR | 4 | 5 |
| XNOR | 5 | 4 |
NAND and NOR mirror each other. What is cheap from one is dear from the other, and the table shows it row by row.
Why real chips love NAND
Inside a CMOS chip, a gate is built from transistors. A NAND gate is the natural shape: a 2-input NAND takes 4 transistors, while a 2-input AND is a NAND followed by a NOT, which takes 6. So chip designers think in NANDs and NORs, and the tools that turn a design into gates do the same.
How do you make a NOT gate from a single 2-input NAND gate?
Show the answer
Answer: D. With A on both inputs the NAND gives (A·A)', which is just A'. Tying one input to 1 would also work, but tying it to 0 would force the output to 1. An unconnected input on a real chip can float to any value, so it is never safe.
2.5 Gates on real chips
Real gates come packed several to a chip. The 7400 series puts two to six gates in one package, with two pins for power. Every chip has a datasheet that says which pin does what and how fast it is.
Today most logic lives inside large chips, such as processors and FPGAs, where millions of gates share one piece of silicon. But the small logic chips of the 7400 series are still made. They are the easiest way to build real gates on a breadboard and watch them work.
The 7400: four NANDs in one package
The first chip of the family, the 7400, holds four 2-input NAND gates in a 14-pin dual in-line package. Its pinout says which pin is which:
| Pin | Name |
|---|---|
| 1 | 1A |
| 2 | 1B |
| 3 | 1Y |
| 4 | 2A |
| 5 | 2B |
| 6 | 2Y |
| 7 | GND |
| 8 | 3Y |
| 9 | 3A |
| 10 | 3B |
| 11 | 4Y |
| 12 | 4A |
| 13 | 4B |
| 14 | VCC |
Other members of the family hold the other gates. The 7402 holds four NOR gates, the 7404 six inverters, the 7408 four AND gates, the 7432 four OR gates and the 7486 four XOR gates. Modern versions add letters, as in 74HC00, which name the family of transistors inside. Volume 09 compares those families.
Assuming every chip in the family has the same pinout. The 7402 NOR chip puts its outputs on pins 1, 4, 10 and 13, where the 7400 has inputs. Wire a 7402 as if it were a 7400 and outputs will fight inputs. Always check the datasheet for the exact part you are using.
Two rules for real chips
- Power every chip. A logic chip does nothing until VCC and GND are connected, even though no circuit diagram of gates shows those pins.
- Never leave an input unconnected. An open input on a CMOS chip floats to any voltage, often into the forbidden zone from Volume 00. Tie every unused input to 0 or to 1.
Delays add up
A datasheet also gives each gate's propagation delay. For a 74HC chip running from 5 V it is roughly ten nanoseconds; inside a modern processor, a gate switches in picoseconds. When gates are joined one after another, their delays add:
a chain of 5 gates at 10 ns each takes 50 ns
The longest chain of gates in a circuit decides how fast it can go. The course on static timing analysis is all about that chain.
On a 7400 chip, which pins must be connected before any of its gates will work?
Show the answer
Answer: B. Pin 14 is VCC and pin 7 is GND. Without power, no gate in the chip can drive its output, whatever its inputs are.
What you learned
- AND gives 1 only when all inputs are 1, OR when at least one is 1, and NOT inverts. They are written A·B, A + B and A'.
- In logic, + means OR, so 1 + 1 = 1.
- NAND and NOR are AND and OR with the output inverted; a bubble on a symbol always means "invert".
- XOR gives 1 when its inputs differ, XNOR when they are the same, and a many-input XOR gives the parity of its inputs.
- To find a circuit's truth table, add a column for each gate and work from the inputs to the output.
- Every gate has a propagation delay, and delays can make glitches that no truth table shows.
- NAND alone can build every gate, and so can NOR: AND takes 2 NANDs, OR takes 3 and XOR takes 4.
- Logic chips need power on their VCC and GND pins, unused inputs must be tied, and pinouts must be checked in the datasheet.
Key words from this volume
Every word below has a plain-English entry in the glossary.
- AND gate
- OR gate
- NOT gate (inverter)
- Bubble
- NAND gate
- NOR gate
- XOR gate
- XNOR gate
- Parity bit
- Propagation delay
- Glitch
- Universal gate
- 7400 series
- DIP (dual in-line package)
- Pinout
- Datasheet
Practice
A truth table from a formula
Write the truth table of Y = (A + B)·C'. Work it column by column.
Show the solution
Add a column for A + B and one for C', then AND those two:
| A | B | C | A + B | C' | Y |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 0 |
| 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 | 1 |
| 0 | 1 | 1 | 1 | 0 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 | 0 | 0 |
| 1 | 1 | 0 | 1 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 | 0 |
Y is 1 on three rows: whenever A or B is 1 and C is 0.
An alarm rule
An alarm should sound when a door is open (D = 1) and the system is armed (S = 1), or when the panic button is pressed (P = 1). Write the formula and say which gates it needs.
Show the solution
"Door open and armed" is D·S, and "or panic" ORs P onto it:
Y = D·S + P
That is one 2-input AND gate and one 2-input OR gate - the same shape as Figure 2.3.
Build OR from NOR gates
Using only 2-input NOR gates, build an OR gate. How many do you need?
Show the solution
A NOR is an OR with its output inverted. So invert it again with a second NOR whose inputs are joined: the first gives (A + B)', and the second gives ((A + B)')' = A + B. That is 2 NOR gates, which matches the table in sub-module 2.4.
Flip some bits
You want to invert the two middle bits of 1011 and leave the others alone. What do you XOR it with, and what is the result?
Show the solution
A 1 in the mask flips a bit and a 0 leaves it, so the mask is 0110:
1011 XOR 0110 = 1101
Interview corner
Why are NAND and NOR called universal?
"Why are NAND and NOR called universal gates?"
Show the solution
"Because each one alone can build every other gate, and so any circuit at all. A NAND with its inputs joined is a NOT; a NAND followed by a NOT is an AND; and a NAND with both inputs inverted is an OR. Once you have NOT, AND and OR you can build anything. The minimum counts are small: 2 NANDs for AND, 3 for OR, 4 for XOR.
They also matter in practice. In CMOS, NAND and NOR are the natural gate shapes - a 2-input NAND is 4 transistors, while an AND is 6 because it needs an inverter on the end."
A NOT gate from an XOR
"You have only 2-input XOR gates. How do you make an inverter?"
Show the solution
"Tie one input to 1. XOR with 1 flips the other input, so A ⊕ 1 = A'. Tie it to 0 instead and the gate just passes A through. That switchable inversion is why XOR gates appear in adders that can also subtract."
Volume 03 gives these gates an algebra of their own, so that a circuit can be simplified on paper before a single gate is placed.