# From a truth table to a real circuit

*The recipe that turns a spec into gates*

Any truth table becomes a circuit by writing one AND term for each row whose output is `1` and then OR-ing those terms together, the mechanical recipe that connects a specification to a buildable gate network.

Group: Gates
URL: https://digiwleea.wleeaf.dev/learn/truth-table-to-circuit/

> **KEY:** A [truth table](https://digiwleea.wleeaf.dev/learn/truth-tables/) tells you *what* a circuit must do; the [canonical forms](https://digiwleea.wleeaf.dev/learn/canonical-forms/) lesson showed the standard shapes an expression can take. This bridge joins them into a single, always-works procedure: given any table, produce gates. After this you never have to guess a circuit, you can derive one.

The gap that feels big: a truth table is a *list of desired answers*, but a circuit is *wires and gates*. How do you get from one to the other without cleverness or luck? The reassuring answer is that there is a recipe, and it works for every table, even one you have never seen. It may not give the smallest circuit (that is what [Karnaugh maps](https://digiwleea.wleeaf.dev/learn/karnaugh/) are for), but it always gives a correct one.

## The recipe

1. Look only at the rows where the output is `1`. Ignore the `0` rows entirely.
2. For each `1` row, write one AND term that is true for exactly that input combination: use the variable if its value is `1` in that row, and its inverse (NOT) if its value is `0`.
3. OR all those AND terms together. The result is `1` on precisely the rows you selected and `0` everywhere else.
4. Build it: an inverter for each NOTed input, an AND gate per term, and one OR gate combining them.

This is the **sum of products** you saw named in canonical forms, now used as a construction method. Each AND term is a product that fires on one exact input pattern; OR-ing them (the sum) lights the output for any of the patterns you wanted. Because every `1` row gets its own dedicated term, the circuit cannot be wrong: it outputs `1` on exactly those rows and nothing else.

A worked example, the XOR function (output `1` when the inputs differ):

| A | B | F |
| --- | --- | --- |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |

_The output is 1 on two rows: (A=0, B=1) and (A=1, B=0)._

Two `1` rows, so two AND terms. Row `A=0, B=1` gives `(NOT A) AND B`. Row `A=1, B=0` gives `A AND (NOT B)`. OR them: `F = ((NOT A) AND B) OR (A AND (NOT B))`. That expression is exactly XOR, and it maps directly to two inverters, two AND gates, and one OR gate. You just built a circuit from nothing but the table.

> **WARN:** The recipe gives a *correct* circuit, not the *smallest* one. Its term count grows with the number of `1` rows, so a table with many ones yields a bulky network. Always run the result through simplification (Boolean algebra or a [Karnaugh map](https://digiwleea.wleeaf.dev/learn/karnaugh/)) before building for real. Also watch the inversions: use NOT for an input that is `0` in that row, not `1`, a very common sign error.

**Q (Try it):** A 2-input function outputs `1` only on the single row `A=1, B=1`. Apply the recipe. What expression and gate do you get?

**A:** One `1` row means one AND term. Both inputs are `1` in that row, so no inversions: the term is `A AND B`. With a single term there is nothing to OR, so `F = A AND B`. The recipe hands you a plain AND gate, which is correct, that row is exactly where AND outputs `1`.

### FAQ

**Q:** How do you turn a truth table into a circuit?

**A:** Write one AND term for each row whose output is `1` (using NOT on any input that is `0` in that row), then OR all the terms together. This sum-of-products expression maps directly to inverters, AND gates, and one OR gate, and is correct for any table.

**Q:** Does this recipe give the smallest circuit?

**A:** No. It always gives a correct circuit, but the number of AND terms equals the number of `1` rows, so it can be large. Simplify the expression with Boolean algebra or a Karnaugh map before building to reduce the gate count.

**Q:** What is sum of products?

**A:** It is an expression written as an OR (the sum) of AND terms (the products), where each AND term is true for one specific input combination. It is the standard form the truth-table-to-circuit recipe produces.

> **KEY:** You can now build a circuit for any specification. Next, minimize those circuits with the [Karnaugh map](https://digiwleea.wleeaf.dev/learn/karnaugh/) so you build the fewest gates that still match the table.
