---
title: Boolean algebra and gates
heading: Logic you can wire
eyebrow: C7 · Lesson 4 · Boolean algebra and gates
xp: 50
---

::: hook
George Boole wrote down an algebra of true and false in 1854, eighty years before anyone built a computer. Claude Shannon noticed in 1937 that relay circuits obey exactly that algebra, so you could design switching circuits with equations, simplify the equations, and build fewer switches. Every chip since is designed that way: with gates that compute AND, OR and NOT, and algebra that rearranges them.

::: play
- Two switches feed every common gate at once. Click A and B to flip them, and watch the lamps.
- Which gates light for exactly one input being 1? For both? For neither?
- Compare NAND with AND, and XNOR with XOR. What does the bubble on a gate's output do?
:::
:::

```sim logic
input A, B
output AND, OR, XOR, NAND, NOR, XNOR, NOTA
assign AND = A & B
assign OR = A | B
assign XOR = A ^ B
assign NAND = ~(A & B)
assign NOR = ~(A | B)
assign XNOR = ~(A ^ B)
assign NOTA = ~A
table: on
```

::: predict
## Which expression is the same as ¬(A·B)?

- [ ] ¬A·¬B
- [x] ¬A + ¬B
- [ ] A + B
- [ ] ¬A ⊕ ¬B

::: reveal
¬A + ¬B. "Not both" is the same as "at least one is false": that's **De Morgan's law**. Its twin, ¬(A + B) = ¬A·¬B, says "neither" is "both are false". De Morgan's laws let you push NOTs through ANDs and ORs, swapping one for the other, which is how designers turn any logic into NAND gates alone.
:::
:::

::: nameit
## Boolean algebra

**The gates**, with A·B for AND, A + B for OR, A ⊕ B for XOR and ¬A (or A′) for NOT:

| A | B | A·B | A + B | A ⊕ B | ¬(A·B) | ¬(A + B) |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 |

A bubble on a gate's output means NOT: NAND, NOR, XNOR.

**Laws** (each also holds with · and + swapped, and 0 and 1 swapped):

- identity: A·1 = A, A + 0 = A; null: A·0 = 0, A + 1 = 1;
- idempotence: A·A = A; complement: A·¬A = 0, A + ¬A = 1;
- absorption: A + A·B = A, A·(A + B) = A;
- distribution: A·(B + C) = A·B + A·C, and also A + B·C = (A + B)·(A + C);
- De Morgan: ¬(A·B) = ¬A + ¬B, ¬(A + B) = ¬A·¬B.

**Universal gates.** NAND alone can build anything: NOT A = NAND(A, A), AND is NAND followed by NOT, and OR is NAND of the inverted inputs (De Morgan). NOR is universal too. Chips are built mostly from NANDs and NORs because in CMOS they're the simplest gates (lesson 7).

**XOR** is "one or the other but not both": A ⊕ B = A·¬B + ¬A·B. It's the sum bit of an adder and a parity checker.
:::


::: example
## Simplify $A + \bar AB$ algebraically.

::: solution
1. **Distribute OR over AND.**
   $$\begin{aligned} A + \bar AB &= (A + \bar A)(A + B) \\ &= 1 \cdot (A + B) \\ &= A + B \end{aligned}$$
2. **Check it in words.** If A is 1 the output is 1 anyway; if A is 0, $\bar A = 1$ and the term is just B.

**Answer:** $A + \bar AB = A + B$.
:::
:::

::: example
## Write $\overline{AB + C}$ with inversions only on single variables.

::: solution
1. **De Morgan on the OR.**
   $$\overline{AB + C} = \overline{AB} \cdot \bar C$$
2. **De Morgan on the AND.**
   $$\overline{AB} \cdot \bar C = (\bar A + \bar B)\bar C$$

**Answer:** $\overline{AB + C} = (\bar A + \bar B)\bar C$.
:::
:::

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

::: solution
1. **Spot the redundant term.** The term BC is redundant: whenever B and C are both 1, either A is 1, and AB is 1, or A is 0, and $\bar AC$ is 1.
2. **Show it algebraically.**
   $$\begin{aligned} BC &= BC(A + \bar A) \\ &= ABC + \bar ABC \end{aligned}$$
   and each piece is absorbed: ABC into AB and $\bar ABC$ into $\bar AC$.
3. **Drop it.**
   $$AB + \bar AC + BC = AB + \bar AC$$
   This is the **consensus theorem**.
4. **When to keep it.** In hardware the "redundant" term isn't always useless: it covers the moment A switches, and lesson 19 adds it back to remove a glitch.

**Answer:** $AB + \bar AC + BC = AB + \bar AC$.
:::
:::

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

## Write ¬(A·B) without a NOT applied to anything but a single input.

::: right
Right. De Morgan: ¬A + ¬B.
:::

::: misses
- [~A & ~B] That's ¬(A + B): "neither", which is NOR. NAND is "not both".
- [any] Not yet. "Not both" means at least one of them is 0.
:::
:::

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

## Simplify A + A·B.

::: right
Right. Absorption: whenever A·B is 1, A is already 1, so A + A·B = A.
:::

::: misses
- [A | B] A + B is 1 when only B is 1; A + A·B isn't.
- [A & B] A·B misses the case A = 1, B = 0.
- [any] Not yet. Try the four rows of the truth table.
:::
:::

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

## Write XOR using only AND, OR and NOT.

::: right
Right. One or the other, but not both.
:::

::: misses
- [A | B] That's OR, which is also 1 when both are 1.
- [~(A & B)] That's NAND, which is also 1 when both are 0.
- [any] Not yet. XOR is 1 when exactly one input is 1.
:::
:::

::: check
given: A = 0 | 1; B = 0 | 1
answer: 1 - (A + B - A*B)
tolerance: 0.01

## With A = {A} and B = {B}, what does a NOR gate output?

::: right
Right: {answer}. NOR is 1 only when both inputs are 0.
:::

::: misses
- [A + B - A*B] That's OR; NOR is its opposite.
- [any] Not yet. NOR = NOT(A OR B).
:::
:::

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

## Factor A·B + A·C.

::: right
Right. Distribution: A·(B + C).
:::

::: misses
- [A & B & C] That needs both B and C.
- [any] Not yet. Pull out the common A.
:::
:::

::: 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 covered by the other two.
:::

::: misses
- [any] Not yet. When B and C are both 1, one of the other two terms is already 1.
:::
:::

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

## Write $\overline{A(B + C)}$ with inversions only on single variables.

::: right
Right: De Morgan twice gives $\bar A + \bar B\bar C$.
:::

::: misses
- [any] Not yet. Break the bar over the AND first, then over the OR.
:::
:::

::: explain
## Why can any logic function be built from NAND gates alone?

::: model
A NAND gate with both inputs tied together is a NOT gate. A NAND followed by a NOT is an AND. And by De Morgan's law, A OR B equals NAND of NOT A and NOT B, so OR is a NAND fed by two NANDs used as inverters. Since NOT, AND and OR can express any truth table (as a sum of products), and each can be made from NANDs, so can any function.
:::
:::

::: practice

::: check
given: A = 0 | 1; B = 0 | 1
answer: 1 - A*B
tolerance: 0.01

## NAND of {A} and {B}?

::: misses
- [A*B] That's AND.
- [any] NOT(A·B).
:::
:::

::: check
given: A = 0 | 1; B = 0 | 1
answer: abs(A - B)
tolerance: 0.01

## XOR of {A} and {B}?

::: misses
- [any] 1 when they differ.
:::
:::

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

## ¬(A + B) as an AND of NOTs?

::: misses
- [~A | ~B] That's NAND.
- [any] De Morgan: ¬A·¬B.
:::
:::
:::
