Comparing Boolean Expressions · 比较布尔表达式
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| equivalent/ɪˈkwɪvələnt/ | 等价 | děng jià |
| De Morgan's Laws/də ˈmɔːɡənz lɔːz/ | 德摩根定律 | dé mó gēn dìng lǜ |
One counterexample defeats a Boolean rewrite
- A proposed rewrite changes !(a && b) into !a && !b. Set a = true and b = false.
- The original is true, while the proposed rewrite is false. De Morgan requires !a || !b, which agrees with the original.
When two conditions mean the same
- Two Boolean expressions are equivalent 等价 if they give the same result for every input. For an integer
x,x >= 5and!(x < 5)are equivalent — both true exactly whenxis at least5. - For Boolean variables without side effects, equivalent expressions have the same result. Also check evaluation order if expressions call methods or change state. 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. For an integer score,
!(score < 60)becomesscore >= 60— same meaning, easier to read. - Removing a double negative or an outer
!often clarifies the intent. Check the exact input domain and evaluation behavior; fewer characters do not guarantee fewer bugs.
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 &&.
For an integer score, which is an 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.
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.
- For two pure Boolean variables there are four input rows, which fully determine the truth function. This does not enumerate the effects of arbitrary method calls or prove every rewrite of a floating-point comparison.
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.
For a=true and b=false, what are the values of !(a && b), !a && !b, and !a || !b, in that order?
The mismatching middle expression is a counterexample to the proposed equivalence.
For a double score equal to NaN, !(score < 60) and score >= 60 return the same Boolean result.
Comparisons score < 60 and score >= 60 are both false for NaN, so the negated first expression is true and the second is false.
If f() returns false, how many times does g() run while evaluating !(f() && g())?
The false first operand short-circuits &&, so g is not called. The equivalent !f() || !g() also skips g.
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. ✓
Carry the reasoning to a new case
- For pure Boolean variables, all four input pairs prove equivalence. De Morgan's forms preserve left-to-right short-circuit evaluation:
!(f() && g())and!f() || !g()both skip g when f is false. - Swapping operands is a different transformation. Although
a && bandb && ahave the same truth table for pure variables,f() && g()andg() && f()may call different methods. For double score=NaN,!(score < 60)is true butscore >= 60is false; the integer comparison rule does not extend to NaN.
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.