Sub topic 2
Truth tables and checking equivalence
This chapter assumes you can convert between binary and decimal, know what the basic gates (AND, OR, NOT, NAND, NOR, XOR, XNOR) do, and have seen the Boolean operators from the previous chapter. Here you will use truth tables as a complete description of a function and as a mechanical way to prove two expressions are the same.
What a truth table is
A Boolean function of n variables maps each of the 2^n input combinations to 0 or 1. A truth table lists every combination (one per row) with the function's output. The table is the function: two expressions that give the same output column describe the same function, however different they look.
Rules for building a table:
- Write the n inputs in a fixed order, such as A, B, C. Treat the first as the most significant bit.
- List the rows in ascending binary order, 000, 001, 010, and so on. Each row's decimal value is its row number, which becomes its minterm index in a later chapter.
- Add intermediate columns for sub-expressions. This avoids errors on longer expressions.
Remember operator precedence when you evaluate: NOT first, then AND, then OR. So AB + A'C means (A·B) + ((A')·C).
Worked example
Take F = AB + A'C. The table below includes the two product terms as helper columns.
| Row | A | B | C | AB | A'C | F |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 | 0 | 1 | 1 |
| 2 | 0 | 1 | 0 | 0 | 0 | 0 |
| 3 | 0 | 1 | 1 | 0 | 1 | 1 |
| 4 | 1 | 0 | 0 | 0 | 0 | 0 |
| 5 | 1 | 0 | 1 | 0 | 0 | 0 |
| 6 | 1 | 1 | 0 | 1 | 0 | 1 |
| 7 | 1 | 1 | 1 | 1 | 0 | 1 |
F is 1 on rows 1, 3, 6 and 7. You can read this off directly as F = Σm(1, 3, 6, 7), a form later chapters develop fully.
You can generate this kind of table with a short program. In Python, 1 - a computes NOT for a 0/1 value. The bitwise ~ does not work here, because ~0 is -1.
from itertools import product
def truth_table(f, names):
print(" ".join(names), "| F")
for v in product([0, 1], repeat=len(names)):
print(" ".join(map(str, v)), "|", f(*v))
truth_table(lambda a, b, c: (a & b) | ((1 - a) & c), ["A", "B", "C"])product([0, 1], repeat=n) yields tuples in ascending binary order, so the rows match the convention above.
Checking equivalence
Two expressions f and g over the same variables are equivalent if and only if they produce the same output on every row. This gives a decision procedure that always terminates:
- Collect all variables that appear in either expression.
- Enumerate all 2^n assignments.
- Evaluate both expressions on each assignment.
- If any row differs, the expressions are not equivalent, and that row is a counterexample. If none differ, they are equivalent.
The following code implements the procedure and checks two identities from the spine: De Morgan's law and absorption.
from itertools import product
def equivalent(f, g, n):
return all(f(*v) == g(*v) for v in product([0, 1], repeat=n))
# De Morgan: NOT(a AND b) == (NOT a) OR (NOT b)
print(equivalent(lambda a, b: int(not (a and b)),
lambda a, b: int((not a) or (not b)), 2))
# Absorption: a + ab == a
print(equivalent(lambda a, b: a | (a & b), lambda a, b: a, 2))Both lines print True.
Finding a counterexample
A version that returns the failing row is more useful when you debug a claimed identity.
from itertools import product
def first_difference(f, g, n):
for v in product([0, 1], repeat=n):
if f(*v) != g(*v):
return v
return None
# Is A + A'B the same as A + B? (yes)
print(first_difference(lambda a, b: a | ((1 - a) & b),
lambda a, b: a | b, 2))
# Is A(B + C) the same as AB + C? (no)
print(first_difference(lambda a, b, c: a & (b | c),
lambda a, b, c: (a & b) | c, 3))The first call prints None (no difference). The second prints (0, 0, 1): with A=0, B=0, C=1, the left side is 0 and the right side is 1.
Proving the consensus term redundant
Consider G = AB + A'C + BC. The extra term BC is 1 only on rows 3 (011) and 7 (111). Row 3 already has F = 1 because A'C = 1. Row 7 already has F = 1 because AB = 1. So BC adds no new 1s, and G has the same output column as F. This is the consensus theorem, and the table proves it without any algebra.
Special cases
- A tautology is an expression whose output is 1 on every row, such as A + A'.
- A contradiction is 0 on every row, such as A·A'.
- Two expressions are equivalent exactly when
f XNOR gis a tautology. Equivalently,f XOR gis a contradiction. - A function implies another (f → g) when every row where f is 1 also has g equal to 1. Check only the rows where f = 1.
Cost and limits
The table has 2^n rows, so the method is exact but doubles in cost with each added variable.
| Variables | Rows | Distinct functions (2^(2^n)) |
|---|---|---|
| 2 | 4 | 16 |
| 3 | 8 | 256 |
| 4 | 16 | 65,536 |
| 5 | 32 | 4,294,967,296 |
Four variables is easy by hand. Twenty variables means over a million rows, which is fine for a program but not for an exam. For exam questions with many variables, use algebra (next chapters) to reduce the expression first, or test a few rows that are most likely to expose a difference.
Common errors
- Wrong precedence. Reading
A + BCas(A + B)Cchanges the function. AND binds tighter than OR. - Wrong row order. If you list rows in a different order, you must remember the mapping when you later read minterm numbers. Stick to binary counting with the first variable as the most significant bit.
- Missing variables. If one expression omits a variable, you still include it in the table. A function that does not depend on C has equal output on rows that differ only in C. Skipping that column can hide a difference.
- Partial checking. Matching on some rows proves nothing. One differing row disproves equivalence, but only all rows prove it.
- Confusing XOR and OR. A + B is 0 only on 00. A ⊕ B is 0 on both 00 and 11.
- Comparing "don't care" outputs. If some outputs are unspecified (covered in a later chapter), two expressions can differ on those rows and still both be valid implementations. Plain equivalence is stricter than that.
Counting facts worth memorizing
- There are 2^n rows for n variables.
- There are 2^(2^n) distinct functions of n variables.
- A function with k ones in its output column has k minterms and 2^n − k maxterms.
- Two different minimal expressions can be equivalent. Equivalence is about the output column, not about the shape of the expression.
Exercise
You want to prove that two expressions over five variables are equivalent by building truth tables. How many rows must you compare, and how many different Boolean functions of five variables exist?
Show answer
You must compare all 2^5 = 32 rows. There are 2^(2^5) = 2^32 = 4,294,967,296 distinct functions of five variables.
Exercise
Is A(B + C) equivalent to AB + C? If not, give a counterexample row.
Show answer
No. With A=0, B=0, C=1, the left side is 0·(0+1) = 0, while the right side is 0 + 1 = 1. Any row with A=0 and C=1 is a counterexample.
Exercise
F = AB + A'C is given. Using the truth table, list its minterms, and state whether F + BC is equivalent to F.
Show answer
F = Σm(1, 3, 6, 7). BC is 1 only on rows 3 and 7, and F is already 1 on both, so F + BC has the same output column as F. They are equivalent.