Binary basics

De Morgan's law explained

Two short rules that move a NOT inside the brackets. See the proof, try both laws live, and use them in gates and code.

Written by
Updated · 7 min read

You have a NOT in front of a bracket, like !(a && b), and you want to get rid of it. De Morgan's laws tell you how: invert every input inside the bracket and swap AND for OR, or OR for AND. So NOT (A AND B) becomes (NOT A) OR (NOT B), and NOT (A OR B) becomes (NOT A) AND (NOT B). Circuit designers use the rules to swap one kind of gate for another, and programmers use them to turn a negated condition into one that reads plainly.

Key takeaways

NOT (A AND B) equals (NOT A) OR (NOT B), and NOT (A OR B) equals (NOT A) AND (NOT B).
Short version: break the bar over the group, invert each input and swap AND with OR.
A truth table with all four input pairs proves both laws.
A NAND gate is an OR gate with inverted inputs, and a NOR gate is an AND gate with inverted inputs.
In code, !(a && b) is the same as !a || !b, which makes range checks easier to read.
binarytranslator.ai

Check both laws yourself

Pick values for A and B and the checker works out both sides of each law. Try A = 1 and B = 0 first, because that's the case where the most common mistake (covered further down) gives the wrong answer.

The two laws in Boolean notation

In Boolean algebra a dot means AND, a plus means OR, and an apostrophe or a bar over a term means NOT. Written that way, the laws are short:

LawExpressionIn words
First law(A · B)' = A' + B'NOT of an AND equals OR of the NOTs
Second law(A + B)' = A' · B'NOT of an OR equals AND of the NOTs

Teachers sum this up as "break the line, change the sign". Picture a bar drawn over the whole of A · B. Break it into a bar over A and a bar over B, and the dot under the break turns into a plus. It works the same way for OR, where the plus turns into a dot.

Proof with truth tables

With two inputs there are only four cases, so the quickest proof is to check all of them. For the first law, the column for (A · B)' and the column for A' + B' are identical:

ABA · B(A · B)'A'B'A' + B'
0001111
0101101
1001011
1110000

For the second law, (A + B)' and A' · B' match in every row:

ABA + B(A + B)'A'B'A' · B'
0001111
0110100
1010010
1110000

If the tables are hard to scan, look for the one row that differs from the rest. In the first table, (A · B)' and A' + B' are both 0 only when A and B are both 1. In the second, (A + B)' and A' · B' are both 1 only when A and B are both 0.

Truth table proof of De Morgan's laws: for all four input pairs, NOT (A AND B) equals NOT A OR NOT B, giving 1, 1, 1, 0, and NOT (A OR B) equals NOT A AND NOT B, giving 1, 0, 0, 0.

Applying the laws step by step

To clear a bar off a group, work from the outside in, because each law only applies to the operator directly under the bar. The outermost operator is the one done last. In (A + B · C)' that's the OR, since AND is done first:

  1. The outer operator is OR, so the second law turns it into A' · (B · C)'.
  2. The bar is now over B · C, an AND, so the first law turns that part into B' + C'.
  3. The result is A' · (B' + C'). Keep the brackets, because without them the AND would be done first. To check it, try A = 0, B = 0, C = 1: the original gives (0 + 0)' = 1 and the result gives 1 · (1 + 0) = 1.

The laws also work for more than two inputs. (A · B · C)' = A' + B' + C', and (A + B + C)' = A' · B' · C'. Every input is inverted and every operator flips, however long the chain is.

The mistake people make most is inverting the inputs and forgetting to swap the operator. You write !a && !b when you meant !(a && b), and the condition comes out false whenever exactly one of a and b is true. With A = 1 and B = 0, NOT (A AND B) is 1, but (NOT A) AND (NOT B) is 0.

De Morgan's law in logic gates

A NAND gate outputs 0 only when both inputs are 1, and so does an OR gate with both inputs inverted. That's the first law drawn as hardware. By the second law, a NOR gate matches an AND gate with inverted inputs. The small circles on gate symbols mean NOT, and designers call moving them from the output to the inputs, or back, bubble pushing. The circuit does the same thing either way.

De Morgan's law in logic gates: a NAND gate equals an OR gate with both inputs inverted, and a NOR gate equals an AND gate with both inputs inverted.

That's why three NAND gates can make an OR gate. A NAND with both inputs tied together acts as a NOT, because 1 NAND 1 is 0 and 0 NAND 0 is 1. Use two of those to get A' and B', feed them into a third NAND, and the first law turns (A' · B')' into A + B. The logic gates guide has a live tester for every gate, so you can check each step.

De Morgan's law in programming

Conditions in code follow the same two rules. In JavaScript, C and Java, !(a && b) gives the same result as !a || !b, and !(a || b) gives the same result as !a && !b. Python uses the words not, and and or, so not (a and b) becomes not a or not b.

You'll use this most on range checks. "x is not between 0 and 9" is !(x >= 0 && x <= 9). The first law turns the AND into an OR and negates each comparison, and the opposite of >= is <. You get x < 0 || x > 9, which says what it means with no NOT in front.

The bitwise operators apply the laws to every bit at once, so ~(a & b) equals ~a | ~b for any two integers. With a = 12 and b = 10, both sides give -9 in Python. You can try other pairs in the XOR calculator, which also runs AND, OR, NAND and NOR.

De Morgan's law in sets and everyday language

Sets follow the same rules, with complement in place of NOT, union in place of OR and intersection in place of AND. Everything outside A ∪ B is outside A and also outside B. Everything outside A ∩ B is outside A or outside B.

Plain English follows them too. "It is not both cold and raining" means "it is not cold, or it is not raining". "I don't want tea or coffee" means "I don't want tea, and I don't want coffee". The laws are named after Augustus De Morgan, a British mathematician who stated them in the 1840s, though the idea was known to logicians long before him.

Practice problems

Rewrite each expression so that NOT applies only to single letters. Answers are in the second column.

ExpressionAnswer
(A · B · C)'A' + B' + C'
(A' + B)'A · B'
(A · B + C)'(A' + B') · C'
((A + B) · C)'A' · B' + C'
!(x > 5 || y == 0)x <= 5 && y != 0

Questions people ask

What does De Morgan's law state?

NOT (A AND B) equals (NOT A) OR (NOT B), and NOT (A OR B) equals (NOT A) AND (NOT B). Inverting a whole AND or OR is the same as inverting each input and swapping the operator.

How do you prove De Morgan's law?

Build a truth table with all four combinations of A and B and compare the two sides. They match in every row, which is a complete proof for Boolean values.

Does De Morgan's law work with three or more variables?

Yes. (A · B · C)' = A' + B' + C' and (A + B + C)' = A' · B' · C'. Every input is inverted and every operator flips.

What is the difference between the first and second law?

The first law starts with a NOT over an AND and ends with an OR. The second starts with a NOT over an OR and ends with an AND. Swap every AND for OR in one law and you get the other.

Why is De Morgan's law important for logic gates?

It shows that NAND is an OR with inverted inputs and NOR is an AND with inverted inputs. Since a NAND or NOR with its inputs tied together is a NOT gate, designers can build any circuit from NAND gates alone, or from NOR gates alone.

Who discovered De Morgan's law?

It is named after Augustus De Morgan, who wrote the rules down in the 1840s. Medieval logicians such as William of Ockham described the same idea in words centuries earlier.

About the authors

Written byUma VictorTechnical writer

Uma Victor is a technical writer and software engineer with seven years of engineering work. He writes API documentation, integration guides and tutorials for developer tools, and his articles have run in Smashing Magazine, freeCodeCamp and LogRocket. He runs the code before he writes about it. On binarytranslator.ai he writes the guides on binary, hex and text encoding.

All guides by UmaLinkedIn

Reviewed byMehran Mozaffari KermaniProfessor of computer engineering, University of South Florida

Mehran Mozaffari Kermani is a professor at the Bellini College of Artificial Intelligence, Cybersecurity and Computing at the University of South Florida. His research covers computer arithmetic, cryptographic hardware and fault detection in digital circuits, and he worked as an ASIC design engineer at AMD before he joined academia. He earned his PhD in electrical and computer engineering at the University of Western Ontario, was a postdoctoral fellow at Princeton and is a senior member of IEEE. On binarytranslator.ai he reviews the logic gate, binary arithmetic and floating-point tools.

ProfileLinkedInHow we review

Keep reading

All posts
In an 8-bit signed integer, 127 + 1 equals -128.Binary basics

MSB and LSB: most and least significant bits, signed integers and overflow

The MSB is the leftmost, highest-value bit and the LSB the rightmost. See what each tells you, how signed integers use the MSB as a sign bit, and how integer overflow wraps values around.9 min read
In floating point, 0.1 + 0.2 equals 0.30000000000000004.Binary basics

Floating point numbers explained: why 0.1 + 0.2 is not 0.3

A floating point number is scientific notation in binary. See why 0.1 + 0.2 is 0.30000000000000004, how precise floats are, float vs double, and how to compare floats and handle money.9 min read
The hexadecimal number 2F3 equals 755 in decimal.Binary basics

What is hexadecimal? The base 16 number system explained

Hexadecimal is base 16, with digits 0 to 9 and A to F. See how place values work, why programmers use hex for bytes, the values worth knowing and how octal compares.7 min read
Scroll to Top