Article
Rewriting Boolean expressions
In maths you can write 3 + 4 as 4 + 3 and the answer does not change. Boolean expressions have rewriting rules too. With them you can turn a long, confusing condition into a short one without changing what it does.
NOT flips a comparison
NOT turns true into false and false into true. When you put it in front of a comparison, you can remove it by switching to the opposite comparison:
| Expression | Equivalent expression without NOT |
|---|---|
NOT (x > 5) | x ≤ 5 |
NOT (x < 5) | x ≥ 5 |
NOT (x ≥ 5) | x < 5 |
NOT (x ≤ 5) | x > 5 |
NOT (x = 5) | x ≠ 5 |
NOT (x ≠ 5) | x = 5 |
The opposite of "more than 5" is "5 or less". The number 5 itself belongs to the opposite side. The table below shows why x < 5 is the wrong partner for NOT (x > 5):
x | x > 5 | NOT (x > 5) | x ≤ 5 | x < 5 |
|---|---|---|---|---|
| 4 | false | true | true | true |
| 5 | false | true | true | false |
| 6 | true | false | false | false |
Only the case x = 5 shows the difference. Try it in Python, which writes not, <= and >=:
The first two lines match. The third does not. Change x to 4 and 6 and run again.
Check your understanding
Which expression is logically equivalent to NOT (score ≥ 50)?
De Morgan's rules
What if NOT sits in front of a bracket with AND or OR inside? Two rules, named after the mathematician Augustus De Morgan, tell you how to rewrite it. In plain words: put NOT on each part, and swap AND with OR.
NOT (a AND b)is equivalent to(NOT a) OR (NOT b).NOT (a OR b)is equivalent to(NOT a) AND (NOT b).
An everyday example: "It is not true that the shop is open and cheap" means "the shop is closed or it is expensive". It only takes one of the two to fail.
Check the second rule with a truth table:
| a | b | a OR b | NOT (a OR b) | (NOT a) AND (NOT b) |
|---|---|---|---|---|
| true | true | true | false | false |
| true | false | true | false | false |
| false | true | true | false | false |
| false | false | false | true | true |
The last two columns match in every row. The same rules work when the parts are comparisons. Rewrite NOT (x > 2 AND x < 8): put NOT on each part, which gives x ≤ 2 and x ≥ 8, and swap AND with OR.
The result is x ≤ 2 OR x ≥ 8, which is equivalent to NOT (x > 2 AND x < 8). Now test the first rule in Python, with a = True and b = False:
Both lines show True. Change a and b to the other three combinations. They always match.
Two smaller rules also help:
- Double NOT cancels:
NOT (NOT a)is equivalent toa. - Order does not matter for
ANDandOR:a AND bis equivalent tob AND a, anda OR bis equivalent tob OR a.
Check your understanding
Which expression is logically equivalent to NOT (a OR b)?
Worked example
Rewrite NOT (age < 13 OR age > 19) without a NOT in front of the bracket. This is the rule for NOT (a OR b):
- Put NOT on each part:
NOT (age < 13)andNOT (age > 19). - Swap OR for AND.
- Remove each NOT by flipping its comparison:
age ≥ 13andage ≤ 19.
The result is age ≥ 13 AND age ≤ 19. Check it with four ages, including the edge values:
age | age < 13 OR age > 19 | NOT (age < 13 OR age > 19) | age ≥ 13 AND age ≤ 19 |
|---|---|---|---|
| 12 | true | false | false |
| 13 | false | true | true |
| 19 | false | true | true |
| 20 | true | false | false |
The last two columns match. The rewritten expression says "age is from 13 to 19".
Common mistakes
- Writing
x < 5forNOT (x > 5). This forgets the value 5 itself. The opposite of>is≤. - Keeping AND when you apply De Morgan.
NOT (a AND b)is not(NOT a) AND (NOT b). You must swap toOR. - Putting NOT on only one part.
NOT (a AND b)is not(NOT a) OR b. Every part gets a NOT. - Forgetting the brackets.
NOT (a AND b)and(NOT a) AND bare different expressions. We write brackets so there is no doubt.
On the exam
Options often include the right rewrite and two or three "almost" rewrites: one forgets to swap AND and OR, one forgets the equal sign, one flips only one part. Use the plain-words rule, then test an edge value such as x = 5 to catch the near misses.
Key words
Negation: putting NOT in front of an expression, which reverses its value.
De Morgan's rules: the two rewriting rules for NOT (a AND b) and NOT (a OR b).
Opposite comparison: the comparison that is true exactly when the original one is false, for example ≤ for >.