Comparing Boolean Expressions
| English | Chinese | Pinyin |
|---|---|---|
| equivalent | 等价 | děng jià |
| De Morgan's Laws | 德摩根定律 | dé mó gēn dìng lǜ |
When two conditions mean the same
- Two Boolean expressions are equivalent 等价 if they give the same result for every input.
x >= 5and!(x < 5)are equivalent — both true exactly whenxis at least5.- Equivalent expressions can be swapped freely without changing behavior.
- A truth table proves equivalence: same output in every row.
De Morgan's Laws
- De Morgan's Laws 德摩根定律 rewrite a NOT over an AND/OR:
!(a && b)equals!a || !b.!(a || b)equals!a && !b.- The pattern: push the
!inside, and flip&&↔||.
Simplifying conditions
- Use De Morgan's and logic rules to write a shorter, clearer equivalent.
!(score < 60)becomesscore >= 60— same meaning, easier to read.- Removing a double negative or an outer
!often clarifies the intent. - Simpler conditions have fewer bugs.
Testing with a truth table
- To check if two expressions are equivalent, tabulate both for all inputs.
- If every row matches, they're equivalent; one mismatch means they're not.
- This is a mechanical, foolproof check when in doubt.
- Especially useful before trusting a "simplification."
When applying De Morgan's Law, you must flip the operator too, not just distribute the !. !(a && b) is !a || !b (AND becomes OR) — writing !a && !b is wrong. The mnemonic: push the NOT in and swap && ↔ ||. When unsure, a truth table settles it in four rows.
Rewriting !(a && b):
- Wrong:
!a && !b. - Right (De Morgan):
!a || !b— AND flips to OR. - Check: if
a = true, b = false, then!(true && false) = !false = true, and!a || !b = false || true = true. ✓
Two Boolean expressions are equivalent if they agree for every input (provable with a truth table). De Morgan's Laws: !(a && b) = !a || !b and !(a || b) = !a && !b — push the ! inward and flip && ↔ ||. Use these to simplify conditions into clearer equivalent forms.
De Morgan: NOT over OR
!(a || b) equals !a && !b — flip the operator.
By De Morgan's Law, !(a && b) is equivalent to...
Push the ! in and flip && to ||.
By De Morgan's Law, !(a || b) is equivalent to...
Push the ! in and flip || to &&.
Which is a simpler equivalent of !(score < 60)?
not-less-than-60 means at-least-60.
Two Boolean expressions are equivalent if they give the same result for every input.
Same output in every truth-table row = equivalent.
Applying De Morgan's Law, !(a && b) becomes !a && !b.
It becomes !a || !b — the operator must flip.