Digital Logic
De Morgan's laws explained
De Morgan's laws say how to push a NOT through a gate: flip every input and swap AND for OR. NOT (A AND B) is the same as NOT A OR NOT B, and NOT (A OR B) is the same as NOT A AND NOT B. They turn the gate you have into the gate you need, and tidy up if-statements in code.
The two laws
In Boolean shorthand, a bar on top means NOT, a dot means AND and a plus means OR:
The first says a NAND is the same as an OR with both inputs inverted. The second says a NOR is the same as an AND with both inputs inverted.
Proof by truth table
With two inputs there are only four rows, so check them all:
| A | B | NOT (A + B) | Ā · B̄ | NOT (A · B) | Ā + B̄ |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 1 | 1 |
| 0 | 1 | 0 | 0 | 1 | 1 |
| 1 | 0 | 0 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 | 0 | 0 |
The pairs match on every row. NOT (A + B) is 1 only when both inputs are 0, and so is Ā · B̄, because both NOTs are 1 only then.
Live experiment
Build NOR with no NOR
The bin has no NOR. Build it from a NOT on A, a NOT on B, an AND or an OR, and a NOT on the output, and the table checks all four rows as you go. Then find NAND the other way too.
“De Morgan's laws” is lesson 2 of 6 in Nand Narrows, Digital Logic World 2. Free with an account. Lessons unlock in order, so you’ll do the 8 before it first.
You already speak it
"I don't want tea or coffee" means not tea and not coffee: NOT (tea OR coffee) = NOT tea AND NOT coffee. English does De Morgan without thinking. Logic's OR always includes both, so the laws only hold for that inclusive OR.
In code
Programmers use the laws to untangle conditions. These two lines test the same thing:
if (!(ready && armed)) { wait(); }
if (!ready || !armed) { wait(); }
So do !(a || b) and !a && !b. The second form is often easier to read: wait if either one isn't ready.
In circuits
Engineers draw a NOT as a small circle, a bubble. De Morgan says a bubble can slide from a gate's output onto all of its inputs, as long as the gate swaps between AND and OR. Designers push bubbles around a schematic until every gate is a NAND or a NOR, the cheapest gates to build in silicon. Augustus De Morgan published the laws in the 1840s, a century before the first electronic computer.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“De Morgan's laws” is lesson 2 of 6 in Nand Narrows, Digital Logic World 2. Free with an account. Lessons unlock in order, so you’ll do the 8 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: Flips both inputs and turns the AND into an OR. NOT (A AND B) = NOT A OR NOT B. Inputs flip, the gate swaps.
- Why not “Does nothing”? The NOT can't simply vanish. To keep the same truth table once it moves inside, both inputs must flip and the AND must become an OR.
- Why not “Removes an input”? No input disappears. Both inputs stay, each with its own NOT.
- Why not “Turns it into XOR”? XOR doesn't appear in De Morgan's laws. NOT (A AND B) becomes NOT A OR NOT B.
Show the answer
Answer: It isn't raining, and it isn't cold. NOT (raining OR cold) = NOT raining AND NOT cold. If either were true, the original sentence would be false.
- Why not “It isn't raining, or it isn't cold”? That's NOT (raining AND cold): it would be true on a rainy warm day, which the original sentence rules out.
- Why not “It's raining and cold”? The sentence says it's not true that either holds, so it can't be raining and cold.
- Why not “It's one or the other”? Being one or the other would make 'raining or cold' true, which the sentence denies.
Show the answer
Answer: !ready || !armed. The NOT moves inside, each part flips, and && becomes ||. If either one is false, the && is false and the ! makes the whole thing true.
- Why not “ready || armed”? The parts have to flip too. ready OR armed gives the wrong answer when both are false and again when both are true.
- Why not “!ready && !armed”? That's the other law's answer, for NOT (ready OR armed). Flipping the parts without swapping AND for OR changes the meaning.
- Why not “ready && !armed”? Only one part is flipped and the AND isn't swapped, so this matches on just some of the rows.
“De Morgan's laws” is lesson 2 of 6 in Nand Narrows, Digital Logic World 2. Free with an account. Lessons unlock in order, so you’ll do the 8 before it first.
Questions people ask
What are De Morgan's laws?
Two rules for moving a NOT through a gate. NOT (A AND B) = NOT A OR NOT B, and NOT (A OR B) = NOT A AND NOT B. Each input flips and AND and OR swap places.
How do you prove De Morgan's laws?
With a truth table. Two inputs have only four combinations, so write both sides of the law for all four rows. They match on every row, which proves the two expressions are the same function.
Do De Morgan's laws work with three or more variables?
Yes. NOT (A AND B AND C) = NOT A OR NOT B OR NOT C, and NOT (A OR B OR C) = NOT A AND NOT B AND NOT C. Flip every input and swap every AND for OR, or every OR for AND.
Related topics
- Logic gates explainedAND, OR, NOT, XOR and their bubbled twins: each one's rule is a truth table.
- Why is NAND called a universal gate?NOT, AND, OR and XOR from one kind of gate, and why chips love it.
- Binary numbers explainedPlaces worth 1, 2, 4 and 8: add up the bits that are on, and four wires count to 15.