Volume 02 Beginner 5 sub-modules ~25 min read

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.

You will learn
  • 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
You need
  • 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.

The AND, OR and NOT gate symbols, each with the formula for its output A A A B B Y = A·B Y = A + B Y = A' AND OR NOT
Figure 2.1 - The three basic gates. AND has a flat back and a round front. OR has a curved back and a pointed front. NOT is a triangle with a small circle, the bubble, which always means 'invert'.

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.

Common mistake

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.

Quick check

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.

The NAND, NOR, XOR and XNOR gate symbols, each with the formula for its output A A A A B B B B Y = (A·B)' Y = (A + B)' Y = A ⊕ B Y = A ⊙ B NAND NOR XOR XNOR
Figure 2.2 - NAND and NOR are AND and OR with a bubble on the output. XOR has an extra curved line across its back, and XNOR is XOR with a bubble.

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.

Quick check

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 circuit that ANDs A with B, then ORs the result with C A B C Y = A·B + C A·B
Figure 2.3 - The AND gate's output, A·B, becomes one input of the OR gate. Working out that middle signal first makes the truth table easy.
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.

Remember

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.

The inputs A, B and C at eight moments, and the output Y = A·B + C A B C Y
Figure 2.4 - Y is high wherever A and B are both high, or C is high. Each column is one row of the truth table.

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 NOT gate whose output follows its input one time step late A Y
Figure 2.5 - The output Y is the opposite of A, but always one step late. That step is the gate's propagation delay.

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:

A, its inverse A' and the AND of the two, with every gate taking one step A A' Y
Figure 2.6 - When A rises, A' falls one step later. In between, both inputs of the AND gate are 1, and its output makes a short pulse that the truth table says can never happen.

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.

Quick check

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.

NOT, AND and OR gates each built only from NAND gates A A A B B Y = A' Y = A·B Y = A + B 1 NAND 2 NANDs 3 NANDs
Figure 2.7 - Top: one NAND with its inputs joined is a NOT. Middle: a NAND followed by a NOT-made-from-NAND is an AND. Bottom: invert both inputs, then NAND them, and the result is an OR.

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.

An XOR gate built from four NAND gates A B Y = A ⊕ B
Figure 2.8 - The first NAND's output feeds both middle NANDs, and the last NAND combines them. The output is A ⊕ B on every row.

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.

Quick check

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:

The pinout of a 7400 quad 2-input NAND chip 1 14 1A VCC 2 13 1B 4B 3 12 1Y 4A 4 11 2A 4Y 5 10 2B 3B 6 9 2Y 3A 7 8 GND 3Y 7400 4 NAND gates A, B: inputs Y: outputs GND, VCC: power
Figure 2.9 - Pins are numbered anticlockwise from the notch, looking down on the chip. Gate 1 takes inputs on pins 1 and 2 and gives its output on pin 3; the other three gates follow the same pattern. Pin 7 is ground and pin 14 is the supply.
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.

Common mistake

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

  1. Power every chip. A logic chip does nothing until VCC and GND are connected, even though no circuit diagram of gates shows those pins.
  2. 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.

Quick check

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

Key words from this volume

Every word below has a plain-English entry in the glossary.

Practice

Practice 1

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.

Practice 2

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.

Practice 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.

Practice 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

Interview question 1

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."

Interview question 2

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.