---
title: "Combinational logic: K-maps, multiplexers, decoders and adders"
heading: Building functions from gates
eyebrow: C7 · Lesson 13 · Combinational logic
xp: 60
---

::: hook
A combinational circuit's outputs depend only on its present inputs: no memory. Adders, multiplexers, decoders and comparators are all combinational, and together they make up the "arithmetic and logic unit" at the heart of a processor. Designing one means getting from a truth table to a small set of gates, and the classic tool for that is the Karnaugh map, which turns algebraic simplification into spotting rectangles.

::: play
- A 2-bit adder: two full-adder stages, where the carry out of the low bit feeds the high bit. Flip the switches to set A (A1 A0) and B (B1 B0).
- Check a few sums against the truth table. Which output is the carry out of the whole adder?
- Follow the carry from the low stage. When does it make a difference to the high bits?
:::
:::

```sim logic
input A1, A0, B1, B0
output S2, S1, S0
assign S0 = A0 ^ B0
assign C0 = A0 & B0
assign S1 = A1 ^ B1 ^ C0
assign S2 = A1 & B1 | C0 & (A1 ^ B1)
table: on
```

::: predict
## A function of three inputs is 1 exactly when C is 1, whatever A and B are. How many of its 8 truth-table rows are 1?

- [ ] 1
- [ ] 2
- [x] 4
- [ ] 8

::: reveal
Four: the rows with C = 1. On a Karnaugh map, those four 1s form a single rectangle covering every combination of A and B, so they simplify to just C. A K-map's job is exactly this: group 1s into the biggest possible rectangles of 1, 2, 4 or 8 cells, each of which becomes one simple product term.
:::
:::

::: nameit
## Designing combinational logic

**From a truth table to a sum of products.** Each row where the output is 1 is a **minterm**: an AND of all inputs, each plain or inverted. OR them together and you have the function. It's correct, but rarely small.

**Karnaugh maps.** Arrange the truth table in a grid whose rows and columns are in Gray-code order (00, 01, 11, 10), so neighboring cells differ in one input, including wrapping around the edges. Then:

1. group the 1s into rectangles whose sides are powers of 2, as large as possible, overlapping if useful;
2. each rectangle is one AND term, keeping only the inputs that stay the same across it;
3. OR the terms.

A group of 2 drops one input, a group of 4 drops two.

**Building blocks:**

- **Multiplexer** (mux): selects one of $2^n$ inputs by an n-bit select. A 2:1 mux is $Y = \bar SA + SB$.
- **Decoder**: n inputs, $2^n$ outputs, exactly one active: used to pick a memory chip or a register.
- **Half adder**: $S = A \oplus B$, $C = AB$. **Full adder**: $S = A \oplus B \oplus C_\text{in}$, $C_\text{out} = AB + C_\text{in}(A \oplus B)$.
- **Ripple-carry adder**: n full adders in a chain, each carry feeding the next; simple but slow (lesson 19).
:::


::: example
## A full adder gets A = 1, B = 1 and carry-in 0. What are its sum and carry?

::: solution
1. **Sum.**
   $$\begin{aligned} S &= A \oplus B \oplus C_\text{in} \\ &= 1 \oplus 1 \oplus 0 \\ &= 0 \end{aligned}$$
2. **Carry.**
   $$\begin{aligned} C_\text{out} &= AB + C_\text{in}(A \oplus B) \\ &= 1 + 0 \\ &= 1 \end{aligned}$$
3. **Check.** 1 + 1 = 10 in binary.

**Answer:** the sum is 0 and the carry is 1.
:::
:::

::: example
## Minimize $\sum m(1, 3, 5, 7)$ for inputs A, B, C.

::: solution
1. **Find the block.** Every odd row is a 1, and the four cells form a block in which A and B both vary.
2. **Read the term.** Only C stays the same across the block, so the function is just C.

**Answer:** $f = C$.
:::
:::

::: example
## Design a majority function of three inputs: 1 when at least two are 1. How many two-input NAND gates does it take?

::: solution
1. **Find the rows.** The output is 1 in rows 3, 5, 6 and 7.
2. **Minimize.** Each 1 pairs with another to give
   $$M = AB + AC + BC$$
   the full adder's carry.
3. **First level of NANDs.** Three NAND2 gates make $\overline{AB}$, $\overline{AC}$ and $\overline{BC}$.
4. **Combine the three.** A NAND of all three gives M, which needs a three-input NAND, or two more NAND2 gates and an inverter:
   $$3 + 3 = 6$$
   gates using only two-input NANDs.
5. **Where it's used.** Majority voting is how triple-redundant systems outvote one failed copy.

**Answer:** $M = AB + AC + BC$, which takes 6 two-input NAND gates.
:::
:::

::: check
kind: boolean
vars: A, B, C
answer: C
ops: and, or, not

## A function of A, B and C is 1 for rows 1, 3, 5 and 7 (counting from 0 with A as the top bit). Write it as simply as possible.

::: right
Right. Those rows are exactly the ones with C = 1.
:::

::: misses
- [A & C] That misses rows 1 and 3, where A = 0.
- [any] Not yet. Write out rows 1, 3, 5 and 7 in binary: what do they share?
:::
:::

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

## A 2:1 multiplexer outputs A when S = 0 and B when S = 1. Write its output.

::: right
Right. $\bar SA + SB$.
:::

::: misses
- [S & A | ~S & B] That's backwards: S = 0 should pick A.
- [A | B] That ignores S.
- [any] Not yet. One term for each value of S.
:::
:::

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

## Simplify $A B \bar C + A B C + \bar A B C$.

::: right
Right. The first two group to AB, the last two to BC: AB + BC, or B(A + C).
:::

::: misses
- [A & B & C] That's only one of the three rows.
- [B] That would also include $\bar A B \bar C$, which isn't in the function.
- [any] Not yet. Group neighbors that differ in one input.
:::
:::

::: check
given: n = 3 | 4 | 5
answer: 2^n
tolerance: 0

## How many outputs does an {n}-input decoder have?

::: right
Right. One for each input combination: {answer}.
:::

::: misses
- [2*n] It's $2^n$, one per combination.
- [any] Not yet. $2^n$.
:::
:::

::: check
given: a = 1..7 int; b = 1..7 int
answer: a + b
base: 2
width: 4

## Add {a:bin3} and {b:bin3} in binary. Give a 4-bit result.

::: right
Right: {answer}.
:::

::: misses
- [any] Not yet. Add column by column from the right, carrying 1 when a column reaches 2.
:::
:::

::: check
kind: boolean
vars: A, B, C
answer: A & B | A & C | B & C
ops: and, or

## Write the three-input majority function.

::: right
Right: $AB + AC + BC$, the full adder's carry.
:::

::: misses
- [any] Not yet. It's 1 in rows 3, 5, 6 and 7.
:::
:::

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

## Write a function that's 1 when A = B and C = D, using XOR, AND and NOT.

::: right
Right: $\overline{A \oplus B}\cdot\overline{C \oplus D}$, a two-bit equality comparator.
:::

::: misses
- [any] Not yet. XOR is 1 when its inputs differ.
:::
:::

::: explain
## Why do neighboring cells in a Karnaugh map differ in only one input, and why does that matter?

::: model
The rows and columns are ordered so that each step changes just one input, including the wrap-around at the edges. Two 1s in neighboring cells then share every input but one, and since that one input appears both plain and inverted, the two minterms combine into a single term without it, by the rule $X\bar Y + XY = X$. Grouping a rectangle of neighbors is therefore the same as applying that algebra repeatedly, but by eye.
:::
:::

::: practice

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

## Simplify $A\bar B + AB$.

::: misses
- [any] The B terms cancel: A.
:::
:::

::: check
given: n = 2 | 3 | 4
answer: n
tolerance: 0

## How many select lines does a $2^{\given{n}}$:1 multiplexer need?

::: misses
- [2^n] The select picks one of $2^n$, so it needs n bits.
- [any] n.
:::
:::

::: check
given: a = 0..3 int; b = 0..3 int
answer: a + b
base: 2
width: 3

## {a:bin2} + {b:bin2} in binary (3 bits)?

::: misses
- [any] Add with carries.
:::
:::
:::
