Lite / Textbooks / Digital Logic and Computer Architecture / Chapter 3
3. Combinational building blocks
Introduction
Engineers rarely design with single gates. They reuse a small set of blocks: the adder that does arithmetic, the multiplexer that chooses between signals and the decoder that points at one of many outputs. This chapter builds each block from gates, and shows that the NAND gate on its own can make all of them.
Figure 3.1. A half adder from two gates (described in words)
Inputs A and B each feed an XOR gate and an AND gate. The XOR output is the sum bit S and the AND output is the carry bit C. A table shows 0+0 gives C=0 S=0, 0+1 and 1+0 give C=0 S=1, and 1+1 gives C=1 S=0.
Explanation
Built from 3 School lessons. Each part is the lesson's own explanation.
3.1 Everything from NAND
If you could only manufacture one kind of gate, could you still build a whole computer? Engineers found the answer is yes, twice.
NOT and AND
NOT A = NAND(A, A). A AND B = NOT(NAND(A, B)), which is two NANDs: one NAND, then a NAND with its inputs tied as a NOT.
OR via De Morgan
De Morgan says A + B = ¬(¬A · ¬B), which is NAND(¬A, ¬B). So OR takes three NANDs: two as NOTs on the inputs, and one to combine them.
XOR in four
Let g1 = NAND(A, B). Then g2 = NAND(A, g1), g3 = NAND(B, g1), and Y = NAND(g2, g3) = A ⊕ B. Check row 11: g1 = 0, g2 = 1, g3 = 1, Y = 0. Row 10: g1 = 1, g2 = 0, g3 = 1, Y = 1.
Why one gate type helps
A factory that makes one well-tested cell can tile it millions of times. Designers still use many gate types today, but the fact that NAND (or NOR) is universal shows how little you need to compute anything.
Common mix-ups
Using only NAND gates always gives the smallest circuit.
✓ It is always possible, not always smallest. Real designs mix gate types for size and speed.
Open the Lite lesson: Everything from NAND
3.2 Half adders and full adders
In binary, 1 + 1 = 10. A computer does that with two gates. How many does it need to add 1 + 1 + 1?
Column addition in binary
Binary adds like decimal, with only two digits: 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 10 (write 0, carry 1), 1 + 1 + 1 = 11 (write 1, carry 1).
The full adder table
For A B Cin = 000, 001, 010, 011, 100, 101, 110, 111, the outputs Cout S are 00, 01, 01, 10, 01, 10, 10, 11. In other words, Cout S is just the count of 1s written in binary.
Chaining adders
To add 4-bit numbers, line up four full adders, one per column. Each carry out feeds the next column's carry in, so the carry ripples from right to left. Next week you will add multi-bit numbers this way and see what happens when the answer is too big.
Derivation: Building the full adder
Half adder (two inputs): the sum is 1 when exactly one input is 1, so S = A ⊕ B; the carry is 1 only when both are 1, so C = A · B.
Full adder (three inputs A, B, Cin): the column total A + B + Cin is 0, 1, 2 or 3, written as two bits Cout S.
S is 1 when the total is odd (1 or 3), which is when an odd number of inputs are 1: S = A ⊕ B ⊕ Cin.
Cout is 1 when the total is 2 or 3. Either A and B are both 1 (A·B), or exactly one of them is 1 and there is a carry in (Cin·(A ⊕ B)). So Cout = A·B + Cin·(A ⊕ B).
This is two half adders and an OR: the first adds A and B, the second adds Cin to that sum, and either carry makes Cout.
Common mix-ups
A half adder is half as accurate as a full adder.
✓ It is exact, but it adds only two bits; the full adder also takes a carry in.Binary 1 + 1 = 2.
✓ 2 is not a binary digit: 1 + 1 = 10, write 0 and carry 1.
Open the Lite lesson: Half adders and full adders
3.3 Multiplexers and decoders: choosing and pointing
A robot has eight sensors but only one analog-to-digital converter. How does it read them all through one input?
Multiplexer: a selector
A 2:1 multiplexer has data inputs A and B and a select input S. If S = 0 the output copies A; if S = 1 it copies B: Y = ¬S·A + S·B. A 4:1 mux uses two select bits; an 8:1 mux uses three.
Decoder: a pointer
A decoder takes an n-bit number and switches on exactly one of 2ⁿ outputs. A 2-to-4 decoder with inputs A1 A0 has outputs D0 = ¬A1·¬A0, D1 = ¬A1·A0, D2 = A1·¬A0, D3 = A1·A0. Give it 10 and only D2 lights.
Where machines use them
A microcontroller uses a mux to connect one of its many analog pins to its single converter. Memory uses decoders to switch on one row out of thousands. A processor uses muxes to choose which register feeds the adder.
Common mix-ups
A multiplexer mixes its inputs together.
✓ It passes exactly one input through, chosen by the select lines.
Open the Lite lesson: Multiplexers and decoders: choosing and pointing
Worked examples
Everything from NAND: example 1
Show that NAND(NAND(A, A), NAND(B, B)) is A OR B.
Solution
NAND(A, A) = ¬A and NAND(B, B) = ¬B. NAND(¬A, ¬B) = ¬(¬A · ¬B) = A + B by De Morgan. Three NANDs make OR.
Half adders and full adders: example 1
Use the full adder equations for A = 1, B = 0, Cin = 1.
Solution
A ⊕ B = 1. S = 1 ⊕ 1 = 0. Cout = A·B + Cin·(A ⊕ B) = 0 + 1·1 = 1. Cout S = 10 = 2, and indeed 1 + 0 + 1 = 2.
Multiplexers and decoders: choosing and pointing: example 1
A 2:1 mux has A = 0, B = 1, S = 1. What is Y?
Solution
Y = ¬S·A + S·B = 0·0 + 1·1 = 1: S = 1 selects B.
Interactive figures, in words
Figure 3.2 from Half adders and full adders
Set 4 bits. Make A = 0001 and B = 0001 and step: the first column is a half adder. Now try A = 0111, B = 0001 and watch the carry ripple through three columns. Then turn on carry in. Fill in the eight-row full adder table in your notebook from what you see.
The adder stepper shows two 4-bit numbers as rows of bit buttons and reveals one column at a time from the right, giving each column's carry in, sum and carry out. Adding 0111 and 0001, the carry ripples through three columns to give 1000. The full adder table for A B Cin from 000 to 111 is Cout S = 00, 01, 01, 10, 01, 10, 10, 11.
Laboratory
These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.
- Silicon Studio: Half adders (Silicon Studio stage)
Build a half adder and confirm all four rows of its table.
Open on the full site - Silicon Studio: Full adders (Silicon Studio stage)
Chain two half adders and an OR gate into a full adder, then test the 1 + 1 + 1 case.
Open on the full site - Silicon Studio: Multiplexers (Silicon Studio stage)
Use a 4-to-1 multiplexer to choose a sensor, and count how many select lines it needs.
Open on the full site - BUILD: Virtual computer, Level 2 (BUILD level)
Find where the build's datapath uses an adder and a multiplexer, and say what each one decides.
Open on the full site
Practice
Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.
- How do you make NOT from a single NAND?
Answer it in “Everything from NAND” - How many NAND gates does the classic XOR construction use?
Answer it in “Everything from NAND” - A half adder is built from…
Answer it in “Half adders and full adders” - For a full adder with A = 1, B = 1, Cin = 1, read Cout S as a 2-bit binary number. What is its value in decimal?
Answer it in “Half adders and full adders” - How many full adders does an 8-bit ripple-carry adder use (one per column)?
Answer it in “Half adders and full adders” - In a 2:1 multiplexer Y = ¬S·A + S·B, what does S = 0 do?
Answer it in “Multiplexers and decoders: choosing and pointing” - How many select lines does a multiplexer need to choose one of 16 inputs?
Answer it in “Multiplexers and decoders: choosing and pointing”
Engineering applications
Arithmetic units
Every processor's arithmetic logic unit contains adders built from the full adder in this chapter, with multiplexers choosing which result reaches the output.
Memory addressing
A decoder turns an address into exactly one active line, so one row of a memory responds. A 3-to-8 decoder picks one of 2^3 = 8 rows.
Watch (optional)
Streaming video uses a lot of data. Everything you need is in the text.
- “How a transistor works” by Ben Eater
- “How Computers Calculate - the ALU: Crash Course Computer Science #5” by CrashCourse
Summary
- NAND is universal: NOT, AND, OR and XOR can all be built from NAND gates alone.
- A half adder adds two bits: sum = A XOR B, carry = A AND B.
- A full adder also accepts a carry in, so full adders can be chained to add longer numbers.
- A multiplexer with k select lines chooses one of 2^k inputs; a decoder with k inputs activates one of 2^k outputs.
Glossary
- NAND and NOR (universal gates)
- NAND outputs 0 only when all inputs are 1; NOR outputs 1 only when all inputs are 0. Either one alone can build every other logic function. Field Guide
- Half adder
- A half adder adds two single bits and gives a sum bit (A XOR B) and a carry bit (A AND B). Field Guide
- Full adder and ripple-carry adder
- A full adder adds three bits (A, B and a carry in) to give a sum and a carry out. Chaining n of them, each carry out feeding the next carry in, makes an n-bit ripple-carry adder. Field Guide
- Multiplexer
- A multiplexer passes one of several data inputs to a single output, chosen by a binary number on its select lines; 2^k inputs need k select lines. Field Guide
- XOR gate
- An XOR gate outputs 1 when its inputs are different and 0 when they are the same. Field Guide
Project: An all-NAND selector
Design a 2-to-1 multiplexer (output A when S = 0, B when S = 1) using only NAND gates. Count the gates, then check your design against the truth table for all eight input combinations.
Deliverables
- A gate diagram using only NAND
- The gate count
- An eight-row truth table check
Write it up on paper or in your own notes. Lite never stores what you write.
References
- David Money Harris and Sarah L. Harris (2012). Digital Design and Computer Architecture, 2nd edition. Morgan Kaufmann.
- Noam Nisan and Shimon Schocken (2021). The Elements of Computing Systems: Building a Modern Computer from First Principles, 2nd edition. MIT Press. Link