Combinational logic: adders, multiplexers and decoders
How gates combine into circuits that do useful work: adding binary numbers, choosing between signals and selecting one device out of many.
Lesson 5 of 6 · 30 min · Self-paced · Free
Building blocks from gates
Gates on their own make simple decisions. Combine them and you get circuits that add numbers, route signals and pick which device on a bus should listen. These circuits sit inside every microcontroller: the processor's arithmetic unit is built from adders, and the logic that selects a memory location or a peripheral is built from decoders and multiplexers.
All the circuits in this lesson are combinational: their outputs depend only on their current inputs. Change the inputs and, after a short delay, the outputs follow. They have no memory of what happened before. Circuits with memory, called sequential circuits, are the subject of the next lesson.
Watch
before you read on
A short, verified video on exactly this idea. Pause whenever you want, then carry on with the explanation below.
Shows step by step how a handful of gates add binary numbers, using real chips you can buy.
→ Watch how one-bit adders chain their carries together; this lesson's half and full adders are the building blocks.
13 min 38 s · captions on YouTube · source
Nothing loads from YouTube until you press play.
Adding binary numbers by hand
Binary addition follows the same column method as decimal, but carries happen whenever a column reaches 2:
0 + 0 = 0
0 + 1 = 1
1 + 1 = 10 (write 0, carry 1)
1 + 1 + 1 = 11 (write 1, carry 1)Adding 5 (0101) and 3 (0011):
carries 1 1 1
0 1 0 1 (5)
+ 0 0 1 1 (3)
---------
1 0 0 0 (8)The rightmost column is 1 + 1 = 10, so write 0 and carry 1. The next is 0 + 1 + 1 = 10 again, and so on.
The half adder
Start with the smallest piece: adding two single bits, A and B. There are two outputs, the sum bit S and the carry bit C:
A B | C S
0 0 | 0 0
0 1 | 0 1
1 0 | 0 1
1 1 | 1 0Compare these columns with the gate truth tables from the last lesson. The sum is 1 when the inputs are different: that is XOR. The carry is 1 only when both are 1: that is AND.
S = A ⊕ B
C = A · BTwo gates, and you have a circuit that adds. It is called a half adder because it cannot accept a carry coming in from a previous column.
The full adder
Every column except the first must also add the carry from the column to its right. A full adder adds three bits: A, B and a carry in, Cin. It produces a sum S and a carry out, Cout:
S = A ⊕ B ⊕ Cin
Cout = A · B + Cin · (A ⊕ B)Read the carry expression in words: there is a carry out if A and B are both 1, or if exactly one of them is 1 and a carry came in. The sum is 1 when an odd number of the three inputs are 1, which is exactly what a chain of XORs computes.
A full adder can be built from two half adders and an OR gate, which is two XORs, two ANDs and one OR in total.
The ripple-carry adder
To add 4-bit numbers, chain four full adders. Each one's carry out feeds the next one's carry in, from the least significant bit to the most. The carry "ripples" along the chain, giving the name ripple-carry adder. The final carry out is a fifth bit: if it is 1, the result was too big for four bits.
Here is a ripple-carry adder simulated in Python, bit by bit:
def full_adder(a, b, carry_in):
s = a ^ b ^ carry_in
carry_out = (a & b) | (carry_in & (a ^ b))
return s, carry_out
def ripple_add(a_bits, b_bits):
"""Add two equal-length bit lists, least significant bit first."""
carry, result = 0, []
for a, b in zip(a_bits, b_bits):
s, carry = full_adder(a, b, carry)
result.append(s)
return result, carry
def to_bits(n, width):
return [(n >> i) & 1 for i in range(width)] # LSB first
def show(bits):
return "".join(str(b) for b in reversed(bits)) # print MSB first
for x, y in [(5, 3), (9, 6), (12, 7)]:
total, carry = ripple_add(to_bits(x, 4), to_bits(y, 4))
print(f"{show(to_bits(x, 4))} + {show(to_bits(y, 4))} = "
f"carry {carry}, sum {show(total)} ({x} + {y} = {x + y})")Output:
0101 + 0011 = carry 0, sum 1000 (5 + 3 = 8)
1001 + 0110 = carry 0, sum 1111 (9 + 6 = 15)
1100 + 0111 = carry 1, sum 0011 (12 + 7 = 19)The last line is worth studying. 12 + 7 = 19 does not fit in four bits (the maximum is 15). The four sum bits show 0011, which is 3, and the carry out is 1, worth 16: 16 + 3 = 19. If a program ignores that carry, it gets 3, which is how integer overflow happens in real processors.
Speed: why ripple is slow
Every gate takes a little time to respond, called its propagation delay. In a ripple-carry adder, the top bit cannot be correct until the carry has passed through every stage below it. A 32-bit ripple adder must wait for the carry to cross 32 full adders. Real processors use cleverer carry-lookahead designs that work out the carries in parallel, using more gates to save time. It is a classic engineering trade-off: area against speed.
Multiplexers: choosing a signal
A multiplexer, or mux, is a digital selector switch. A 2-to-1 mux has two data inputs, A and B, one select input S, and one output Y. When S is 0 the output copies A; when S is 1 it copies B:
Y = S′ · A + S · BRead it as: "(not S and A) or (S and B)". Only one of the two AND terms can be active at a time, so the select line decides which input gets through.
With two select lines you can choose among four inputs (a 4-to-1 mux); with three, among eight. In general, n select lines choose among 2ⁿ inputs. Microcontrollers use multiplexers everywhere. A single analogue-to-digital converter is often shared between many input pins by a mux, and many pins can be switched between several internal functions, such as plain input/output or a serial port, by multiplexers controlled from software.
Decoders: selecting one of many
A decoder does the reverse job: it takes an n-bit binary number and turns on exactly one of 2ⁿ outputs. A 2-to-4 decoder with inputs A1 and A0:
A1 A0 | Y0 Y1 Y2 Y3
0 0 | 1 0 0 0
0 1 | 0 1 0 0
1 0 | 0 0 1 0
1 1 | 0 0 0 1
Y0 = A1′ · A0′ Y1 = A1′ · A0 Y2 = A1 · A0′ Y3 = A1 · A0Each output is a single AND gate that recognises one pattern. Decoders are how a processor selects one memory chip or one peripheral out of many: the address bits go into a decoder, and only the device whose output is active responds. A display driver that lights one digit at a time uses the same idea.
Designing your own
The method for any combinational circuit is the same:
- Write the specification in words.
- Turn it into a truth table covering every input combination.
- Write an expression for each output, for example by OR-ing together an AND term for each row where the output is 1.
- Simplify with Boolean algebra.
- Check the simplified version against the truth table.
Project
Paper and Python project: build and test an adder
- Fill in the full 8-row truth table of a full adder by hand, from the definition (add three bits, write the two-bit result).
- Check that the expressions S = A ⊕ B ⊕ Cin and Cout = A · B + Cin · (A ⊕ B) give the same table.
- Extend the Python ripple adder in this lesson to 8 bits, and write a loop that checks every pair of numbers from 0 to 255 against Python's own
+(remember to include the carry out as bit 8). - Write a Python function for a 2-to-1 multiplexer using only
&,|and1 - s, and test all 8 combinations of A, B and S. - Draw the gate diagram of a 2-to-4 decoder.
No hardware is needed for this project.
Deliverables
- Full adder truth table checked against the expressions
- An 8-bit ripple adder that passes all 65,536 tests
- A tested multiplexer function
- A decoder gate diagram
Knowledge check
Check your understanding
Go further
- Wikipedia — Adder (electronics) ↗
Half adders, full adders, ripple-carry and faster carry-lookahead designs, with truth tables.
Lesson materials · free to download
Sign in to track your progress. Reading is free without an account.