---
title: Canonical forms and NAND logic
heading: Minterms, maxterms, and building everything from one gate
eyebrow: C7 · Lesson 5 · Canonical forms and NAND logic
xp: 40
---

::: hook
Any Boolean function can be written straight from its truth table, with no cleverness at all: OR together one AND term for every row where it's 1. That **canonical** form is usually far from the simplest, but it always exists, it's unique, and it's the starting point for every simplification method. Turn it over with De Morgan's laws and the same function becomes a network of nothing but NAND gates, which is why a chip can be built from a single kind of gate.
:::

::: play
- Two circuits for the same function: F written as a sum of products, $AB + \bar AC$, and G built from NAND gates only.
- Flip the inputs, or read the truth table: F and G agree in every row.
- The NAND version is two levels of NAND where the sum of products had AND then OR: De Morgan's laws turn one into the other.
:::
:::

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

::: predict
## How many different Boolean functions of three inputs are there?

- [ ] 8
- [ ] 24
- [ ] 64
- [x] 256

::: reveal
256. A truth table for three inputs has 8 rows, and each row's output can be 0 or 1 independently, so there are $2^8$ = 256 different tables. With four inputs there are $2^{16}$ = 65,536.
:::
:::

::: nameit
## Minterms and maxterms

For n inputs, each row of the truth table has a **minterm**: the AND of every input, inverted where the row has a 0. Row 5 of a three-input table (A = 1, B = 0, C = 1) has minterm $m_5 = A\bar BC$, which is 1 only in that row. Every function is the OR of the minterms of its 1 rows, its **canonical sum of products**:

$$f = \sum m(1, 3, 6, 7) = \bar A\bar BC + \bar ABC + AB\bar C + ABC$$

Each row also has a **maxterm**: the OR of every input, inverted where the row has a 1, which is 0 only in that row: $M_5 = \bar A + B + \bar C$. Every function is the AND of the maxterms of its 0 rows, its **canonical product of sums**: $f = \prod M(0, 2, 4, 5)$. The two lists always split the rows between them.

## Useful theorems

- **De Morgan:** $\overline{A + B} = \bar A\bar B$ and $\overline{AB} = \bar A + \bar B$.
- **Consensus:** $AB + \bar AC + BC = AB + \bar AC$; the term BC is covered by the other two.
- **Duality:** swap AND with OR and 0 with 1, and any true identity stays true.

## One gate is enough

A sum of products is two levels, AND then OR. By De Morgan, $XY + ZW = \overline{\overline{XY} \cdot \overline{ZW}}$: NAND then NAND. So any function in SOP form becomes **NAND–NAND** logic with the same structure, and a product of sums becomes **NOR–NOR**. NAND alone (or NOR alone) is **functionally complete**: NOT is a NAND with its inputs tied, AND is a NAND followed by a NOT, and OR is a NAND of inverted inputs. CMOS builds NAND and NOR more cheaply than AND and OR (lesson 8), so real logic is full of them.
:::

::: example
## A function of A, B, C is 1 in rows 1, 3, 6 and 7. Write its canonical SOP and POS, then simplify.

::: solution
1. **Canonical SOP.**
   $$\bar A\bar BC + \bar ABC + AB\bar C + ABC$$
2. **Canonical POS.**
   $$\prod M(0, 2, 4, 5)$$
3. **Simplify.** Rows 1 and 3 share $\bar AC$, rows 6 and 7 share AB, so
   $$f = \bar AC + AB$$
   the function in the sim.

**Answer:** the SOP is $\bar A\bar BC + \bar ABC + AB\bar C + ABC$, the POS is $\prod M(0, 2, 4, 5)$, and both simplify to $f = \bar AC + AB$.
:::
:::

::: example
## Build $f = AB + \bar AC$ from two-input NAND gates only. How many?

::: solution
1. **Make $\bar A$.** One NAND with both inputs tied to A makes $\bar A$.
2. **Make the two products, inverted.** Two more NANDs make $\overline{AB}$ and $\overline{\bar AC}$.
3. **Combine them.** A final NAND of those two gives $AB + \bar AC$.

**Answer:** four two-input NAND gates.
:::
:::

::: example
## Write $A \oplus B \oplus C$ in canonical sum-of-products form. Can it be simplified?

::: solution
1. **Find the rows.** The three-input XOR is 1 when an odd number of inputs are 1: rows 1, 2, 4 and 7.
2. **Write the canonical SOP.**
   $$\begin{aligned} A \oplus B \oplus C &= \sum m(1, 2, 4, 7) \\ &= \bar A\bar BC + \bar AB\bar C \\ &\quad + A\bar B\bar C + ABC \end{aligned}$$
3. **Try to combine terms.** No two of those minterms differ in just one variable, so no pair combines: the sum of products stays at four three-literal terms.
4. **Use XOR gates.** Two XOR gates do the same job, which is why parity and adder circuits use XOR rather than AND–OR logic.

**Answer:** $\bar A\bar BC + \bar AB\bar C + A\bar B\bar C + ABC$, which can't be simplified as a sum of products.
:::
:::

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

## Write a function of A, B, C equal to $\sum m(1, 2, 7)$ (rows counted from 0 with A as the top bit).

::: right
Right. $\bar A\bar BC + \bar AB\bar C + ABC$, or anything equivalent.
:::

::: misses
- [~A & ~B & C | ~A & B & ~C] That's rows 1 and 2; row 7 is ABC.
- [any] Not yet. One AND term per listed row.
:::
:::

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

## Write a function of A, B, C equal to $\prod M(0, 5)$.

::: right
Right. $(A + B + C)(\bar A + B + \bar C)$: 0 in rows 0 and 5, 1 everywhere else.
:::

::: misses
- [~A & ~B & ~C | A & ~B & C] That's the sum of minterms 0 and 5: 1 in those rows instead of 0.
- [any] Not yet. Each maxterm is 0 in exactly one row.
:::
:::

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

## Simplify $AB + \bar AC + BC$.

::: right
Right. The consensus term BC is redundant: $AB + \bar AC$.
:::

::: misses
- [A & B | C] That would be 1 for $A\bar BC$, which none of the terms covers.
- [any] Not yet. When is BC = 1 but neither AB nor $\bar AC$?
:::
:::

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

## Write $\overline{A + BC}$ using inversions only on single variables.

::: right
Right. $\bar A(\bar B + \bar C)$, by De Morgan twice.
:::

::: misses
- [~A | ~B & ~C] De Morgan turns the OR into an AND: $\bar A \cdot \overline{BC}$.
- [any] Not yet. Apply De Morgan to the OR, then to BC.
:::
:::

::: check
given: n = 2 | 3 | 4
answer: 2^(2^n)
tolerance: 0.1%

## How many different Boolean functions of {n} inputs are there?

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

::: misses
- [2^n] That's the number of rows; each row's output can be chosen freely.
- [any] Not yet. Count the possible truth tables.
:::
:::

::: check
given: n = 3 | 4 | 5; k = 3 | 5
answer: 2^n - k
tolerance: 0.1%

## A function of {n} inputs has {k} minterms. How many maxterms does its canonical product of sums have?

::: right
Right. Every other row: $2^n - k$ = {answer}.
:::

::: misses
- [k] Maxterms cover the 0 rows, not the 1 rows.
- [any] Not yet. Rows split between minterms and maxterms.
:::
:::

::: check
kind: choice

## Which single gate type can build any Boolean function on its own?

- [ ] AND
- [ ] OR
- [x] NAND
- [ ] XOR

::: misses
- [1] AND can't make an inversion.
- [2] OR can't make an inversion either.
- [4] XOR with a constant 1 inverts, but XOR can't make AND.
:::
:::

::: check
kind: boolean
vars: A, B, C
answer: A ^ B ^ C
ops: xor

## Write $\sum m(1, 2, 4, 7)$ using only XOR.

::: right
Right: the odd-parity function, $A \oplus B \oplus C$.
:::

::: misses
- [any] Not yet. Count the 1s in each listed row.
:::
:::

::: check
kind: derivation
vars: n = 1..10

## A function has n inputs.

::: step
answer: 2^n

### How many rows does its truth table have?
:::

::: step
answer: 2^(2^n)

### How many different functions of n inputs are there?
:::

::: step
answer: n*2^n/2

### How many literals does the n-input parity function's canonical SOP have in all? (It has $2^{n-1}$ minterms of n literals.)
:::

::: right
Right. Parity is the worst case for sum of products: nothing combines.
:::
:::

::: explain
## Why can any truth table be built as a sum of products?

::: model
A minterm is an AND of every input, each inverted or not, chosen so it's 1 in exactly one row of the truth table and 0 in all the others. OR together the minterms of every row where the function should be 1: in each of those rows exactly one minterm is 1, so the OR is 1, and in every other row all the minterms are 0, so the OR is 0. That reproduces the table exactly, whatever it is. It's rarely the simplest circuit, but it proves every function can be built from AND, OR and NOT.
:::
:::

::: practice

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

## Write $\sum m(1, 2)$ for inputs A, B.

::: misses
- [any] One AND term per row: 01 and 10.
:::
:::

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

## Write $\overline{ABC}$ using inversions only on single variables.

::: misses
- [any] De Morgan: the AND becomes OR.
:::
:::

::: check
given: n = 3 | 4 | 6
answer: 2^n
tolerance: 0.1%

## How many minterms does a function of {n} inputs have available?

::: misses
- [any] One per row.
:::
:::
:::
