Unit 3: Programming · Logical equivalence

Article

Rewriting Boolean expressions

7 min read

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:

ExpressionEquivalent 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):

xx > 5NOT (x > 5)x ≤ 5x < 5
4falsetruetruetrue
5falsetruetruefalse
6truefalsefalsefalse

Only the case x = 5 shows the difference. Try it in Python, which writes not, <= and >=:

Runs in your browser

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:

aba OR bNOT (a OR b)(NOT a) AND (NOT b)
truetruetruefalsefalse
truefalsetruefalsefalse
falsetruetruefalsefalse
falsefalsefalsetruetrue

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:

Runs in your browser

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 to a.
  • Order does not matter for AND and OR: a AND b is equivalent to b AND a, and a OR b is equivalent to b 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):

  1. Put NOT on each part: NOT (age < 13) and NOT (age > 19).
  2. Swap OR for AND.
  3. Remove each NOT by flipping its comparison: age ≥ 13 and age ≤ 19.

The result is age ≥ 13 AND age ≤ 19. Check it with four ages, including the edge values:

ageage < 13 OR age > 19NOT (age < 13 OR age > 19)age ≥ 13 AND age ≤ 19
12truefalsefalse
13falsetruetrue
19falsetruetrue
20truefalsefalse

The last two columns match. The rewritten expression says "age is from 13 to 19".

Common mistakes

  • Writing x < 5 for NOT (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 to OR.
  • 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 b are 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 >.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.