A CPU's adder can add, but the CPU has many registers holding numbers, and the adder has only two inputs. Something has to pick which two registers feed the adder for this instruction, and which register the answer goes into. That picking is done by two small circuits: the multiplexer, which chooses one of several inputs, and the decoder, which turns a number into "switch on this one line out of many".
A railway station's points (track switches) are a good comparison: many tracks, one platform, and a signal that decides which train comes through. This lesson explains multiplexers and decoders, their demultiplexer and encoder cousins, how they're built from gates, and where each appears inside a computer.
What you need first: logic gates and truth tables and half adder and full adder.
Key terms
- Multiplexer (MUX): a circuit that chooses one of several input lines and passes it to a single output.
- Select lines: the control bits that say which input a multiplexer picks. n select lines can choose between 2ⁿ inputs.
- Demultiplexer (DEMUX): the reverse of a MUX: sends one input to one of several outputs.
- Decoder: turns an n-bit number into 2ⁿ output lines, with exactly one of them set to 1.
- Encoder: the reverse of a decoder: turns "line k is on" into the number k.
- Enable: an extra input that turns the whole circuit on or off.
The 2-to-1 multiplexer
A 2-to-1 MUX has two data inputs, A and B, one select line S, and one output Y:
- If S = 0, Y = A.
- If S = 1, Y = B.
| S | A | B | Y |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 1 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 1 |
A shorter way to show the same thing:
| S | Y |
|---|---|
| 0 | A |
| 1 | B |
As a Boolean expression:
Y = S'·A + S·B
Read it as: "(NOT S and A) or (S and B)". When S = 0, the first AND gate lets A through and the second blocks B; when S = 1, the opposite. So a 2-to-1 MUX is one NOT gate, two AND gates and one OR gate.
def NOT(x): return 1 - x
def mux2(a, b, s):
return (NOT(s) & a) | (s & b)
for s in (0, 1):
for a in (0, 1):
for b in (0, 1):
print(s, a, b, "->", mux2(a, b, s))
0 0 0 -> 0
0 0 1 -> 0
0 1 0 -> 1
0 1 1 -> 1
1 0 0 -> 0
1 0 1 -> 1
1 1 0 -> 0
1 1 1 -> 1
The output copies A when S is 0 and B when S is 1, exactly as the table says. In Python code, you'd write the same choice as y = b if s else a. A multiplexer is the hardware version of that line.
The 4-to-1 multiplexer
With two select lines (S1, S0), there are four combinations, so a MUX can choose between four inputs, I0 to I3:
| S1 | S0 | Y |
|---|---|---|
| 0 | 0 | I0 |
| 0 | 1 | I1 |
| 1 | 0 | I2 |
| 1 | 1 | I3 |
Y = S1'S0'·I0 + S1'S0·I1 + S1S0'·I2 + S1S0·I3
Each AND term is 1 only for its own select pattern. You can also build it from three 2-to-1 MUXes: two choose within the pairs (I0/I1 and I2/I3) using S0, and a third picks between those two results using S1.
def mux2(a, b, s):
return ((1 - s) & a) | (s & b)
def mux4(i0, i1, i2, i3, s1, s0):
low = mux2(i0, i1, s0)
high = mux2(i2, i3, s0)
return mux2(low, high, s1)
inputs = (0, 1, 1, 0) # I0..I3
for s1 in (0, 1):
for s0 in (0, 1):
print(f"select {s1}{s0} -> I{2 * s1 + s0} = {mux4(*inputs, s1, s0)}")
select 00 -> I0 = 0
select 01 -> I1 = 1
select 10 -> I2 = 1
select 11 -> I3 = 0
The pattern scales: 3 select lines choose among 8 inputs, and 6 select lines among 64. The select lines are simply the binary number of the input you want.
Choosing whole numbers, not just bits
A MUX that chooses between two 8-bit numbers is just eight 2-to-1 MUXes side by side, all sharing the same select line. Inside a CPU, "which register goes into the adder?" is answered by a wide MUX like this, with the register number from the instruction connected to its select lines.
The demultiplexer
A DEMUX does the reverse: one input D goes to one of several outputs, chosen by the select lines, and the other outputs stay 0. With 2 select lines, a 1-to-4 DEMUX:
| S1 | S0 | Y0 | Y1 | Y2 | Y3 |
|---|---|---|---|---|---|
| 0 | 0 | D | 0 | 0 | 0 |
| 0 | 1 | 0 | D | 0 | 0 |
| 1 | 0 | 0 | 0 | D | 0 |
| 1 | 1 | 0 | 0 | 0 | D |
A MUX lets many sources share one wire; a DEMUX delivers what's on that wire to the right destination. Old telephone exchanges and data links used the pair to send many conversations down one cable, which is where the name "multiplexing" comes from.
The decoder
A decoder takes an n-bit number and switches on exactly one of its 2ⁿ outputs: the one with that number. A 2-to-4 decoder:
| 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 |
Each output is one AND gate: Y0 = A1'A0', Y1 = A1'A0, Y2 = A1A0', Y3 = A1A0. (A DEMUX is a decoder whose AND gates also take the data input D.)
def decoder(bits):
n = len(bits)
value = int("".join(map(str, bits)), 2)
return [1 if i == value else 0 for i in range(2 ** n)]
print(decoder([1, 0]))
print(decoder([1, 1, 0]))
[0, 0, 1, 0]
[0, 0, 0, 0, 0, 0, 1, 0]
Input 10 (2) switches on output 2; input 110 (6) switches on output 6 of 8.
Decoders pick memory locations
Every byte of RAM has an address. When the CPU asks for address 6, a decoder (a much larger one) switches on the single row of memory cells at address 6, so only that row is read or written. The same idea selects which register to write a result into, and which part of the CPU handles an instruction: the control unit decodes each instruction's opcode into "turn on the adder" or "turn on the comparator" lines.
The encoder
An encoder does the reverse of a decoder: if input line k is on, it outputs the number k in binary. A 4-to-2 encoder turns 0100 (line 2 on) into 10. A priority encoder handles several lines being on at once by reporting the highest one. A keyboard controller works this way: it reports which key was pressed as a number, and if two keys are pressed together, priority rules decide.
Putting them together: a tiny ALU
The ALU (arithmetic logic unit) in a CPU does several operations on the same two inputs at once (add, AND, OR, XOR) and a multiplexer picks which result to keep, based on the instruction. Here's a 4-operation, 8-bit version, using the adder from the previous lesson:
def add8(x, y):
return (x + y) & 0xFF # an 8-bit adder: keep only 8 bits
def alu(x, y, op):
results = [add8(x, y), x & y, x | y, x ^ y] # every circuit works in parallel
return results[op] # the MUX: op is the 2-bit select value
names = ["ADD", "AND", "OR", "XOR"]
for op in range(4):
print(f"{names[op]:<3}", format(alu(0b1100_1010, 0b0101_0110, op), "08b"))
ADD 00100000
AND 01000010
OR 11011110
XOR 10011100
In real hardware, all four circuits compute at the same time on every clock tick, and the MUX simply lets one answer through. The 2-bit op comes from the instruction, via the control unit's decoder. This is the pattern from logic gates to a CPU builds on.
Practice
- A 2-to-1 MUX has A = 1, B = 0. What's the output when S = 0, and when S = 1?
Show the answer
S = 0 gives A, which is 1. S = 1 gives B, which is 0.
- How many select lines does a 16-to-1 multiplexer need? And a decoder with 8 outputs, how many inputs?
Show the answer
4 select lines, because 2⁴ = 16. A decoder with 8 outputs needs 3 inputs, because 2³ = 8.
- Write the output pattern of a 3-to-8 decoder for the input
011.
Show the answer
Only output 3 is on: 0 0 0 1 0 0 0 0 (Y0 to Y7).
- What's the difference between a multiplexer and a demultiplexer?
Show the answer
A multiplexer chooses one of many inputs and sends it to one output. A demultiplexer takes one input and sends it to one of many outputs. The select lines choose in both cases.
- Add a fifth operation, "subtract", to the tiny ALU and work out
alu(10, 3, 4). How many select bits does the MUX now need?
Show the answer
def alu(x, y, op):
results = [(x + y) & 0xFF, x & y, x | y, x ^ y, (x - y) & 0xFF]
return results[op]
print(alu(10, 3, 4))
7
Five operations need 3 select bits, because 2 bits only give 4 choices and 3 bits give 8.
Interview questions
What is a multiplexer, and how many select lines does an N-to-1 MUX need?
A multiplexer is a combinational circuit that routes one of N data inputs to a single output based on select lines. It needs log₂N select lines, so an 8-to-1 MUX needs 3.
What is the difference between a decoder and a demultiplexer?
A decoder converts an n-bit input code into 2ⁿ outputs with exactly one active. A demultiplexer routes a data input to one of 2ⁿ outputs chosen by select lines. A decoder with an enable input acts as a demultiplexer, with the enable used as the data line.
Can a multiplexer implement any Boolean function?
Yes. Connect the function's input variables to the select lines and wire each data input to the constant 0 or 1 from the truth table; a 2ⁿ-to-1 MUX then implements any n-variable function. FPGAs use this idea in their lookup tables.
Key takeaways
- A multiplexer picks one of 2ⁿ inputs using n select lines; a 2-to-1 MUX is
Y = S'A + SB. - A demultiplexer sends one input to one of several outputs.
- A decoder turns an n-bit number into one active line out of 2ⁿ; an encoder does the reverse.
- CPUs use MUXes to route registers into the ALU and pick its result, and decoders to select memory addresses, registers and instructions.
- In an ALU, every operation is computed at once and a MUX keeps the one the instruction asked for.
Frequently asked questions
Why is it called a multiplexer?
"Multiplex" means sending several signals over one channel. A multiplexer chooses which of several signals uses the single output line at any moment; switching quickly between them lets many signals share one wire.
Is a multiplexer like an if statement?
Yes. y = b if s else a is a 2-to-1 multiplexer, and a list lookup like results[op] is a larger one. The hardware version computes every option at once and picks one; the software version usually computes only the chosen one.
Where are decoders used in real life?
Selecting memory rows by address, choosing which register or chip to enable, decoding CPU instructions, and driving seven-segment displays, where a decoder turns a digit's binary code into the segments to light.
What to learn next
- Assemble everything into a processor in from logic gates to a CPU.
- Revise the adder the ALU uses in half adder and full adder.
- Revise the gates in logic gates and truth tables.