# De Morgan's Laws

> 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.

Page: https://formulabro.yunusemrevurgun.com/computer-science/de-morgans-laws/  
Category: Computer Science  
Published: 2026-10-22  
Updated: 2026-10-22

## Formula

$$
\neg(A \land B) = \neg A \lor \neg B \qquad \neg(A \lor B) = \neg A \land \neg B
$$

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

- $A$: A statement that is either true or false, such as "it is raining".
- $B$: A second statement that is either true or false, such as "it is cold".
- $\neg$: NOT. It flips a statement: true becomes false and false becomes true.
- $\land$: AND. The result is true only when both statements are true.
- $\lor$: 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 $\neg(A \land B)$ is true. Here $A$ means it is raining and $B$ means it is cold. So the tour runs unless it is both raining and cold.

1. Apply the first law: $\neg(A \land B) = \neg A \lor \neg B$. In words, the tour runs when it is not raining, or it is not cold.
2. In code, `if not (raining and cold):` becomes `if (not raining) or (not cold):`. Both lines give the same answer every time.
3. Row 1, not raining and not cold ($A$ false, $B$ false): left side $\neg(F \land F) = \neg F = T$. Right side $\neg F \lor \neg F = T \lor T = T$. They match.
4. Row 2, not raining and cold ($A$ false, $B$ true): left side $\neg(F \land T) = \neg F = T$. Right side $\neg F \lor \neg T = T \lor F = T$. They match.
5. Row 3, raining and not cold ($A$ true, $B$ false): left side $\neg(T \land F) = \neg F = T$. Right side $\neg T \lor \neg F = F \lor T = T$. They match.
6. Row 4, raining and cold ($A$ true, $B$ true): left side $\neg(T \land T) = \neg T = F$. Right side $\neg T \lor \neg T = F \lor F = F$. 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.

## Common mistakes

- Moving the NOT but forgetting to flip the AND or OR. $\neg(A \land B)$ is not $\neg A \land \neg B$, and the two can give different answers.
- Dropping the brackets. $\neg A \land B$ flips only A, while $\neg(A \land B)$ 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 `if` statements 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](https://formulabro.yunusemrevurgun.com/computer-science/binary-number-value/)
- [Binary Search Steps](https://formulabro.yunusemrevurgun.com/computer-science/binary-search-steps/)

## Sources

- [De Morgan's laws, Wikipedia](https://en.wikipedia.org/wiki/De_Morgan%27s_laws)
- [Truth table, Wikipedia](https://en.wikipedia.org/wiki/Truth_table)

From Formula Bro (https://formulabro.yunusemrevurgun.com/), a side project developed by yunusemrevurgun.com.
