De Morgan's Laws and How to Use Them
Learn how "not" passes through "and" and "or", with truth tables, set pictures, quantifiers and cleaner program conditions.
Key takeaways
- ¬(P ∧ Q) is equivalent to ¬P ∨ ¬Q, and ¬(P ∨ Q) is equivalent to ¬P ∧ ¬Q.
- To apply either law, negate every part and flip the connective between them.
- In sets, the complement of a union is the intersection of the complements, and the reverse holds too.
- The same idea turns "not all" into "some are not" and "there is no" into "all are not".
De Morgan's laws tell you how to negate a combined statement. The opposite of "P and Q" is "not P, or not Q". The opposite of "P or Q" is "not P, and not Q". In both cases you negate each part and swap "and" with "or".
The same pattern shows up in set theory, in statements about "all" and "some", in computer code and in digital circuits. Learn it once and you can reuse it in all of those places.
The two laws
In symbols, where ¬ means not, ∧ means and, ∨ means (inclusive) or, and ≡ means "is logically equivalent to":
¬(P ∧ Q) ≡ ¬P ∨ ¬Q
¬(P ∨ Q) ≡ ¬P ∧ ¬Q The laws are named for Augustus De Morgan, a British mathematician and logician of the nineteenth century. The underlying idea was known to logicians well before him, but his statement of it in the era of newly symbolic logic is why his name stuck.
In plain English, suppose a café says you get a discount if you are a student and it is Monday. When do you miss out? When you are not a student, or it is not Monday. You do not need to fail both tests. Failing either is enough.
Now suppose entry is free if you are a member or under twelve. You pay only if you are not a member and also not under twelve. Here you must fail both.
Checking the laws with a truth table
A truth table settles the matter by listing all cases. T means true and F means false.
| P | Q | P ∧ Q | ¬(P ∧ Q) | ¬P | ¬Q | ¬P ∨ ¬Q |
|---|---|---|---|---|---|---|
| T | T | T | F | F | F | F |
| T | F | F | T | F | T | T |
| F | T | F | T | T | F | T |
| F | F | F | T | T | T | T |
The columns for ¬(P ∧ Q) and ¬P ∨ ¬Q match in all four rows, so the formulas are equivalent. The second law can be checked the same way: both sides are true only in the bottom row. If building tables is new to you, start with propositional logic and truth tables.
You can also get the second law from the first without another table. Replace P with ¬P and Q with ¬Q in the first law, cancel double negations, and then negate both sides.
The set version
For sets inside some larger universal set U, the complement of A, written Aᶜ, contains everything in U that is not in A. The union A ∪ B holds whatever is in A or B, and the intersection A ∩ B holds whatever is in both. Then:
(A ∪ B)ᶜ = Aᶜ ∩ Bᶜ
(A ∩ B)ᶜ = Aᶜ ∪ Bᶜ Here is a numerical check. Let U = {1, 2, 3, 4, 5, 6, 7, 8}, let A = {1, 2, 3, 4} and let B = {3, 4, 5, 6}.
- A ∪ B = {1, 2, 3, 4, 5, 6}, so its complement is {7, 8}.
- Aᶜ = {5, 6, 7, 8} and Bᶜ = {1, 2, 7, 8}, whose intersection is {7, 8}.
- A ∩ B = {3, 4}, so its complement is {1, 2, 5, 6, 7, 8}.
- Aᶜ ∪ Bᶜ = {1, 2, 5, 6, 7, 8}.
Both pairs agree. One example is not a proof, but the proof is short: an element x is outside A ∪ B exactly when it is not the case that x is in A or x is in B, which by the logical law means x is not in A and not in B. For more on these operations, see set theory basics.
Longer chains and nested formulas
The laws extend to any number of parts. The negation of P ∧ Q ∧ R is ¬P ∨ ¬Q ∨ ¬R. The negation of a long "or" is the long "and" of the negations. For sets, this holds even for infinitely many: the complement of a union of a whole family of sets equals the intersection of their complements.
For nested formulas, work from the outside in. Take ¬((P ∨ ¬Q) ∧ R).
¬((P ∨ ¬Q) ∧ R)
≡ ¬(P ∨ ¬Q) ∨ ¬R first law on the outer "and"
≡ (¬P ∧ ¬¬Q) ∨ ¬R second law on the inner "or"
≡ (¬P ∧ Q) ∨ ¬R double negation Check one case: P false, Q true, R true. The original has P ∨ ¬Q false, so the conjunction is false and its negation is true. The final line has ¬P ∧ Q true, so it is true as well.
Conditionals fit in once you rewrite them. Since P → Q is equivalent to ¬P ∨ Q, its negation is ¬(¬P ∨ Q), which the second law turns into P ∧ ¬Q.
From "all" and "some" to the same pattern
Over a finite collection, "every item is red" is one big "and": item 1 is red and item 2 is red and so on. "Some item is red" is one big "or". So De Morgan's laws suggest, and predicate logic confirms for any domain:
| Statement | Its negation |
|---|---|
| For all x, P(x) | There exists x with not P(x) |
| There exists x with P(x) | For all x, not P(x) |
"Not every prime is odd" means "some prime is not odd", and 2 is that prime. "No square of a real number is negative" means "every square of a real number is not negative". The rules for these symbols are explained in quantifiers in plain English, and long examples appear in how to negate a mathematical statement.
Using the laws in inequalities and code
Many everyday conditions hide an "and". The statement 2 < x ≤ 7 means x > 2 and x ≤ 7. Its negation is therefore x ≤ 2 or x > 7. Notice that each inequality flips between strict and non-strict. Writing "x ≤ 2 and x > 7" is a classic error that describes no numbers at all.
Programmers use the laws to simplify conditions. Many languages write "not" as !, "and" as && and "or" as ||. These two lines test the same thing:
!(isEmpty || isLocked)
!isEmpty && !isLocked One subtlety matters in real programs. When evaluating parts has side effects or can raise errors, the order of the parts still counts, because many languages stop evaluating once the answer is known. De Morgan's rewrite keeps the order, so it is safe as long as you do not also shuffle the terms. Database queries with missing values can follow a three-valued logic, a topic related to many-valued logic.
In circuits
A NAND gate outputs the negation of "and". By the first law it behaves exactly like an "or" gate with both inputs inverted. A NOR gate, the negation of "or", behaves like an "and" gate with inverted inputs. Engineers use this to redraw a circuit with whichever gates are convenient, which is a central move in Boolean algebra for digital circuits.
A counting problem solved by taking the opposite
The set version of the law is a workhorse in counting. Suppose you want to know how many whole numbers from 1 to 100 are divisible by neither 2 nor 5.
Let A be the numbers in that range divisible by 2 and B those divisible by 5. You want the numbers outside A and also outside B, which is Aᶜ ∩ Bᶜ. By De Morgan's law this is (A ∪ B)ᶜ, the complement of the numbers divisible by at least one of the two.
size of A = 50
size of B = 20
size of A ∩ B = 10 multiples of 10
size of A ∪ B = 50 + 20 − 10 = 60 subtract the double count
size of (A ∪ B)ᶜ = 100 − 60 = 40 So 40 numbers avoid both. You can sanity-check this: each block of ten consecutive numbers contains 1, 3, 7 and 9 as the endings that qualify, and four per block over ten blocks gives 40.
The same move works in probability. The chance that none of several events happens equals one minus the chance that at least one does, and the reverse trick is explained in using complements for "at least one" problems.
One caution about other logics
In classical logic both laws hold fully. In intuitionistic logic, which demands explicit constructions, three of the four directions survive. The one that fails in general is going from ¬(P ∧ Q) to ¬P ∨ ¬Q: knowing that both cannot hold does not tell you which one fails.
Common mistakes
- Negating the parts but keeping the connective. "Not (tall and fast)" is not "not tall and not fast".
- Flipping the connective but forgetting a part. Every term inside the brackets must be negated.
- Reversing an inequality wrongly. The negation of x > 3 is x ≤ 3, not x < 3.
- Losing brackets. After applying a law inside a larger expression, keep the new group in brackets so precedence does not change the meaning.
A quick safety test works for any rewrite: choose truth values that make the original true, then confirm the new form is also true. Do the same with values that make it false. With only two or three letters, checking every row takes a minute and removes all doubt.