---
title: Combinational building blocks
heading: Decoders, encoders, multiplexers and comparators
eyebrow: C7 · Lesson 16 · Combinational building blocks
xp: 40
---

::: hook
Nobody designs a processor gate by gate. They assemble it from a handful of standard blocks, each a few gates, each used thousands of times: decoders that turn a number into one active line, encoders that do the reverse, multiplexers that pick one of several inputs, and comparators that test whether two numbers are equal or which is larger. A multiplexer is even a universal block: with the right constants on its data inputs it computes any function of its select lines, which is exactly how FPGAs implement logic (lesson 45).
:::

::: play
- F is a function of A, B and C written as a sum of minterms, $\sum m(1, 2, 6, 7)$. M computes the same function with a 4:1 multiplexer: A and B select one of four data inputs, which are C, $\bar C$, 0 and 1.
- The truth tables match. Each data input is just the function's value for that row pair, written in terms of C.
:::
:::

```sim logic
input A, B, C
output F, M
assign F = ~A&~B&C | ~A&B&~C | A&B&~C | A&B&C
assign M = A ? B : (B ? ~C : C)
table: on
```

::: predict
## How many select lines does a 32-to-1 multiplexer need?

- [ ] 32
- [ ] 16
- [x] 5
- [ ] 1

::: reveal
Five: $2^5 = 32$, so a 5-bit number picks one of the 32 inputs. A decoder runs the other way: 5 inputs light exactly one of 32 outputs.
:::
:::

::: nameit
## Decoders and encoders

An **n-to-$2^n$ decoder** activates exactly the output whose number is on its inputs: output k is the minterm $m_k$. With an **enable** input E, every output is ANDed with E, which lets two 2:4 decoders and an inverter make a 3:8 decoder (the top address bit enables one or the other). Decoders select memory chips, registers and instructions.

An **encoder** does the reverse, turning one active line of $2^n$ into its n-bit number. A **priority encoder** handles several active inputs by reporting the highest-priority one, with a **valid** output for "any input active". For four inputs with $I_3$ highest: $Y_1 = I_3 + I_2$, $Y_0 = I_3 + \bar I_2I_1$, valid $= I_3 + I_2 + I_1 + I_0$.

## Multiplexers

A **$2^n$:1 multiplexer** routes data input k to its output when the select lines hold k: $Y = \sum_k m_k(S)D_k$. Larger ones are built as trees of 2:1 muxes.

**As a function generator:** any function of n variables is a $2^n$:1 mux with the truth table's outputs (0s and 1s) on its data inputs, a **lookup table**. Better, a $2^{n-1}$:1 mux driven by n − 1 of the variables, with each data input set to 0, 1, the last variable or its complement. A **demultiplexer** sends one input to the selected one of $2^n$ outputs: a decoder with its enable used as the data input.

## Comparators

**Equality:** two n-bit numbers are equal when every bit pair matches: $EQ = \prod_i \overline{A_i \oplus B_i}$. **Magnitude:** A > B when, scanning from the top bit, the first differing bit has $A_i = 1$. For two bits:

$$A > B = A_1\bar B_1 + \overline{A_1 \oplus B_1}\,A_0\bar B_0$$

In practice a subtractor's borrow does the same job (lesson 17).
:::

::: example
## Implement $f(A, B, C) = \sum m(1, 2, 6, 7)$ with a 4:1 mux selected by A and B.

::: solution
1. **Rows 0 and 1 (AB = 00).** They give f = 0, 1, so $D_0 = C$.
2. **Rows 2 and 3 (AB = 01).** They give 1, 0, so $D_1 = \bar C$.
3. **Rows 4 and 5 (AB = 10).** They give 0, 0, so $D_2 = 0$.
4. **Rows 6 and 7 (AB = 11).** They give 1, 1, so $D_3 = 1$.

**Answer:** $D_0 = C$, $D_1 = \bar C$, $D_2 = 0$ and $D_3 = 1$: the circuit M in the sim.
:::
:::

::: example
## Build a 4-to-16 decoder from 2-to-4 decoders with enables. How many?

::: solution
1. **First level.** One 2:4 decoder on the top two bits drives the enables of four more.
2. **Second level.** The four 2:4 decoders on the bottom two bits each produce four of the sixteen outputs.
3. **Count them.**
   $$1 + 4 = 5$$

**Answer:** five 2-to-4 decoders.
:::
:::

::: example
## An 8-input priority encoder (input 7 has the highest priority) sees the inputs 0010 1100, input 7 on the left. What does it output?

::: solution
1. **Find the active inputs.** Reading 0010 1100 with input 7 on the left, inputs 5, 3 and 2 are active.
2. **Pick the highest.** The highest-numbered active input wins, so the output is 5, binary 101, and the valid flag is 1; inputs 3 and 2 are ignored.
3. **Where it's used.** Interrupt controllers use exactly this: several devices can request at once, and the encoder reports the most urgent.
4. **No input active.** The valid flag goes to 0, which tells the reader that the 000 on the output means nothing.

**Answer:** 101 (input 5), with the valid flag at 1.
:::
:::

::: check
given: n = 3 | 5 | 8
answer: 2^n
tolerance: 0.1%

## How many outputs does a decoder with {n} inputs have?

::: right
Right. $2^n$ = {answer}.
:::

::: misses
- [n] Each output is one combination of the inputs.
- [any] Not yet. One output per input combination.
:::
:::

::: check
given: N = 4 | 16 | 64 | 256
answer: log(N)/log(2)
tolerance: 0.1%

## How many select lines does an {N}:1 multiplexer need?

::: right
Right. $\log_2 N$ = {answer}.
:::

::: misses
- [N] The select lines form a binary number.
- [any] Not yet. $2^n = N$.
:::
:::

::: check
kind: boolean
vars: E, A, B
answer: E & A & ~B
ops: and, or, not

## A 2:4 decoder with enable E has inputs A (top bit) and B. Write its output $Y_2$.

::: right
Right. $EA\bar B$: input 10 = 2, and only when enabled.
:::

::: misses
- [A & ~B] Include the enable.
- [E & ~A & B] That's $Y_1$: A is the top bit.
- [any] Not yet. Output 2 is AB = 10, ANDed with E.
:::
:::

::: check
kind: boolean
vars: I3, I2, I1, I0
answer: I3 | ~I2 & I1
ops: and, or, not

## A priority encoder has inputs $I_3$ (highest priority) down to $I_0$. Write its low output bit $Y_0$.

::: right
Right. $Y_0 = I_3 + \bar I_2I_1$: 1 for priority codes 3 and 1.
:::

::: misses
- [I3 | I1] If $I_2$ is active it outranks $I_1$, and the code is 2, with $Y_0$ = 0.
- [any] Not yet. $Y_0$ is 1 when the winner is input 3 or input 1.
:::
:::

::: check
kind: boolean
vars: A1, A0, B1, B0
answer: ~(A1 ^ B1) & ~(A0 ^ B0)
ops: and, or, not, xor

## Write an equality comparator for the 2-bit numbers $A_1A_0$ and $B_1B_0$.

::: right
Right. Both bit pairs must match: $\overline{A_1 \oplus B_1} \cdot \overline{A_0 \oplus B_0}$.
:::

::: misses
- [~(A1 ^ B1) | ~(A0 ^ B0)] Both pairs must match, not either one.
- [any] Not yet. XNOR each pair, then AND.
:::
:::

::: check
kind: boolean
vars: A1, A0, B1, B0
answer: A1 & ~B1 | ~(A1 ^ B1) & A0 & ~B0
ops: and, or, not, xor

## Write A > B for the 2-bit numbers $A_1A_0$ and $B_1B_0$.

::: right
Right. The top bits decide unless they're equal; then the bottom bits do.
:::

::: misses
- [A1 & ~B1 | A0 & ~B0] That says 01 > 10, because the low bits win even when the top bits disagree.
- [any] Not yet. Compare the top bits first.
:::
:::

::: check
kind: choice

## A function of A, B and C is built from a 4:1 mux selected by A and B. For the rows with AB = 10 the function is 1 when C = 0 and 0 when C = 1. What goes on data input 2?

- [ ] C
- [x] $\bar C$
- [ ] 0
- [ ] 1

::: misses
- [1] The function is the opposite of C in those rows.
- [3] It isn't 0 in both rows.
- [4] It isn't 1 in both rows.
:::
:::

::: check
kind: boolean
vars: I3, I2, I1, I0
answer: I3 | ~I2 & I1
ops: and, or, not

## A 4-input priority encoder (I3 highest) outputs a 2-bit code $Y_1Y_0$. Write $Y_0$.

::: right
Right: $Y_0 = I_3 + \bar I_2I_1$. It's 1 for input 3, and for input 1 only when input 2 isn't active.
:::

::: misses
- [any] Not yet. $Y_0$ is 1 when the winner is input 3 or input 1.
:::
:::

::: check
kind: parts
given: n = 4 | 6 | 8

## An n-to-$2^n$ decoder with n = {n} is built as a tree of 2-to-4 decoders with enables.

::: part
answer: 2^n
tolerance: 0

### How many outputs does it have?

::: misses
- [any] $2^n$.
:::
:::

::: part
answer: (4^(n/2) - 1)/3
tolerance: 0

### How many 2-to-4 decoders does it need?

::: misses
- [4^(n/2)/4] Count every level of the tree, not just the last.
- [any] $1 + 4 + 16 + \cdots$, one level per two bits.
:::
:::
:::

::: explain
## Why can a multiplexer implement any Boolean function of its select inputs?

::: model
A $2^n$:1 multiplexer routes exactly one of its data inputs to the output, the one numbered by the select lines. If the select lines carry the function's inputs and data input k is wired to the function's value for row k of its truth table, then for every input combination the multiplexer outputs exactly the value the table calls for. The multiplexer is a lookup table, and changing the constants on its data inputs changes the function without changing any wiring: the principle behind FPGAs.
:::
:::

::: practice

::: check
kind: boolean
vars: S, A, B
answer: ~S & A | S & B
ops: and, or, not

## Write a 2:1 mux that outputs A when S = 0 and B when S = 1.

::: misses
- [any] One term for each value of S.
:::
:::

::: check
given: N = 8 | 32 | 128
answer: N - 1
tolerance: 0.1%

## How many 2:1 multiplexers make an {N}:1 multiplexer as a tree?

::: misses
- [any] Each 2:1 mux removes one candidate.
:::
:::

::: check
kind: boolean
vars: A, B
answer: ~(A ^ B)
ops: and, or, not, xor

## Write a 1-bit equality comparator for A and B.

::: misses
- [any] XNOR.
:::
:::
:::
