# Truth tables

*Specifying logic, completely*

A truth table lists the output a logic function produces for every possible combination of its inputs. It is the complete, unambiguous specification of a circuit: what you design from and what you verify against.

Group: Number & logic
URL: https://digiwleea.wleeaf.dev/learn/truth-tables/

[Boolean algebra](https://digiwleea.wleeaf.dev/learn/boolean-algebra/) gives you operations and laws. A **truth table** gives you a way to pin down *exactly* what a piece of logic does: list every possible combination of inputs, and next to each write the output you want. Because a combinational function has finitely many input combinations, the table is **complete**, it says everything, with no ambiguity and nothing left to interpretation. It is the single most useful tool in this whole course.

## Rows: every input combination

With `n` inputs there are `2^n` combinations, so the table has `2^n` rows (this is the same `2^n` from [binary](https://digiwleea.wleeaf.dev/learn/binary/): each input is a bit). The tidy convention is to list the input columns as if they were a binary number **counting up** from all-`0` to all-`1`, so you never miss or repeat a row. For two inputs that is four rows; for three inputs, eight.

| A | B | AND | OR | XOR |
| --- | --- | --- | --- | --- |
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |

_Three functions of the same two inputs, side by side. Read down each output column: AND is 1 only in the all-1 row, OR is 0 only in the all-0 row, and XOR is 1 exactly where the inputs differ._

> **KEY:** The output column **is** the function. Two expressions that look different but fill in the same output column are the same logic, full stop. That is how you *prove* a [boolean-algebra](https://digiwleea.wleeaf.dev/learn/boolean-algebra/) identity or a De Morgan law: build the table for each side and check the output columns match row for row. To do that instantly for any expression, use the [truth table generator](https://digiwleea.wleeaf.dev/tools/truth-table-generator/).

## A truth table as a design starting point

Reading a table tells you what a circuit does. Writing a table first, *then* building to match it, is how you **design**. You describe the behavior you want as a column of `0`s and `1`s, and the structure follows from it. Two ways to turn a table into logic:

1. **Sum of products (from the 1 rows):** for each row whose output is `1`, AND together the inputs (each in true form if it is `1` in that row, inverted if it is `0`), then OR all those terms. Each term fires for exactly one row, so the OR is `1` on precisely the rows you wanted.
2. **For a CMOS gate (from the 0 rows):** the same idea drives transistor design, but you look at the `0` rows to decide when to pull the output low. That is the recipe in [designing gates](https://digiwleea.wleeaf.dev/learn/designing-gates/).

Example, sum of products for XOR: the `1` rows are `A=0,B=1` and `A=1,B=0`, giving `(NOT A AND B) OR (A AND NOT B)`. Fill in that expression's table and you get the XOR column back exactly. The same table can be built many ways (this lesson's XOR, the four-NAND [XOR](https://digiwleea.wleeaf.dev/learn/xor/), the gates of a [half adder](https://digiwleea.wleeaf.dev/learn/halfadder/)); they are equal because they share a truth table.

**Q (Try it):** Write the truth table for a 2-input function `F` that is `1` only when `A` and `B` are equal (both `0` or both `1`). Which single named gate matches it?

**A:** The table is `A,B -> F`: `0,0 -> 1`, `0,1 -> 0`, `1,0 -> 0`, `1,1 -> 1`. `F` is `1` exactly when the inputs are the **same**, which is the opposite of [XOR](https://digiwleea.wleeaf.dev/learn/xor/). It is called **XNOR** (NOT-XOR), the equality detector.

## The lab fills the table for you

When you build a circuit in the lab, the simulator can sweep every input combination and show you its real truth table, so you can compare the circuit you built against the table you intended. The XOR below is one such circuit; its column should be `0,1,1,0`. Open it, toggle the inputs through all four rows, and watch the output match the XOR column above.

_Circuit diagram: An XOR circuit, whose truth table is 0,1,1,0 for inputs 00,01,10,11. Open it in the lab and step the inputs through every row to confirm the circuit fills in the table you expect._

> **KEY:** Truth tables are how you both **specify** and **check** logic for the rest of the course. The very next step is to make a table *small*: a function's first table is often bigger than it needs to be, and [Karnaugh maps](https://digiwleea.wleeaf.dev/learn/karnaugh/) (after the Gates group) read straight off the table to find the simplest circuit. First, though, we turn tables into transistors in [Complementary CMOS](https://digiwleea.wleeaf.dev/learn/cmos/) and [designing gates](https://digiwleea.wleeaf.dev/learn/designing-gates/).

### FAQ

**Q:** What is a truth table?

**A:** A truth table lists the output a logic function produces for every possible combination of its inputs. Because a combinational function has finitely many input combinations, the table is **complete**: it is the unambiguous specification of the circuit, the thing you design from and verify against.

**Q:** How many rows does a truth table have?

**A:** A function with `n` inputs has `2^n` rows, because each input is a bit and there are `2^n` combinations (the same `2^n` from [binary](https://digiwleea.wleeaf.dev/learn/binary/)). So two inputs give four rows and three inputs give eight. List the input columns counting up from all-`0` to all-`1` so you never miss or repeat a row.

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

**A:** Use sum of products: for each row whose output is `1`, AND together the inputs (each in true form where it is `1` in that row, inverted where it is `0`), then OR all those terms. Each term fires for exactly one row, so the OR is `1` on precisely the rows you wanted. A CMOS gate instead reads the `0` rows (see [designing gates](https://digiwleea.wleeaf.dev/learn/designing-gates/)).

**Q:** How do you prove two boolean expressions are equal?

**A:** Build the truth table for each side and compare their output columns row for row: if the columns match, the expressions are the same logic, full stop. The output column **is** the function, which is exactly how you prove a [boolean-algebra](https://digiwleea.wleeaf.dev/learn/boolean-algebra/) identity or a De Morgan law.
