De Morgan's Laws
In one sentence: De Morgan’s laws say that a NOT in front of a group of AND or OR conditions flips the group: AND becomes OR, and OR becomes AND.
Read it out loud: “Not, open bracket, A and B, close bracket, equals not A or not B. And not, open bracket, A or B, close bracket, equals not A and not B.”
What each symbol means
| Symbol | Meaning |
|---|---|
| A statement that is either true or false, such as “it is raining”. | |
| A second statement that is either true or false, such as “it is cold”. | |
| NOT. It flips a statement: true becomes false and false becomes true. | |
| AND. The result is true only when both statements are true. | |
| OR. The result is true when at least one of the statements is true. |
What the formula is doing
Programmers write conditions with three words: NOT, AND and OR. A condition such as not (raining and cold) can be hard to read at a glance. De Morgan’s laws give an equal form that is often easier to understand or to code.
The first law reads: NOT (A AND B) means the same as (NOT A) OR (NOT B). The NOT moves inside the bracket and the AND turns into an OR. The second law works the other way: NOT (A OR B) equals (NOT A) AND (NOT B), so the OR turns into an AND.
The reason is plain English. “Not both” means at least one of them fails, which is the same as saying “at least one is not true”. “Not either” means neither one holds, which means both are not true.
The laws hold for every combination of true and false. You can check that with a truth table that lists all four combinations of A and B, which the example does. Both sides agree in every row, so the two forms are the same condition.
An everyday picture
Think of a club rule: you get in only if you are a member and you are over 18. Being turned away means you are not a member, or you are not over 18. Saying “not both” in the rule turns into “at least one missing” when you turn it around. That is De Morgan’s first law in everyday words.
Worked example: Rewriting a rain-and-cold rule in code
A park app runs a walking tour when is true. Here means it is raining and means it is cold. So the tour runs unless it is both raining and cold.
- Apply the first law: . In words, the tour runs when it is not raining, or it is not cold.
- In code,
if not (raining and cold):becomesif (not raining) or (not cold):. Both lines give the same answer every time. - Row 1, not raining and not cold ( false, false): left side . Right side . They match.
- Row 2, not raining and cold ( false, true): left side . Right side . They match.
- Row 3, raining and not cold ( true, false): left side . Right side . They match.
- Row 4, raining and cold ( true, true): left side . Right side . They match.
Answer: The two forms are the same rule. The tour runs in three of the four weather cases and is off only when it is both raining and cold. Both versions of the code give that same result in all four cases, so you can pick whichever one reads more clearly.
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Moving the NOT but forgetting to flip the AND or OR. is not , and the two can give different answers.
- Dropping the brackets. flips only A, while flips the whole group.
- Reading “not both” as “neither”. “Not both” means at least one fails, while “neither” means both fail.
- Checking only one row of the truth table. A single example can look right by accident, so check all four combinations.
Where you will meet it
- Writing
ifstatements in most programming languages, to make long conditions easier to read. - Digital logic design, where a NAND gate gives the same result as an OR gate with inverted inputs.
- Search and database queries, where a NOT around several conditions is rewritten to make the query clearer.
- Set theory in mathematics, where the complement of an intersection equals the union of the complements.
Questions people ask
What is the difference between AND and OR?
AND is true only when both parts are true. OR is true when at least one part is true. For example, “has a ticket AND is over 12” needs both, while “has a ticket OR has a pass” needs only one.
Why are they called De Morgan's laws?
They are named after Augustus De Morgan, a British mathematician of the 1800s who described them in his work on formal logic.
Do the laws work for more than two statements?
Yes. NOT (A AND B AND C) equals NOT A OR NOT B OR NOT C, and the same pattern works for OR. Each NOT flips one statement and the operator flips too.
Related formulas
- Binary Number Value: Binary uses only the digits 0 and 1, and this formula turns a row of those digits into the ordinary number they stand for.
- Binary Search Steps: Binary search finds an item in a sorted list by checking the middle and throwing away half, and this formula counts how many checks that takes at most.