π2.6: Comparing Boolean Expressions
Table of Contents
π This page is a condensed version of CSAwesome Topic 2.6
Equivalent Boolean Expressions
What if you heard a rumor about a senior at your high school? And then you heard that the rumor wasnβt true - it wasnβt a senior at your high school. Which part of βa senior at your high schoolβ wasnβt true? Maybe they werenβt a senior? Or maybe they didnβt go to your high school? You could write this as a logic statement like below using negation (!
) and the and (&&
) operator since both parts have to be true for the whole statement to be true.
a = "senior"
b = "at our high school"
!(a && b)
// This means it is not true that (a) it is a senior
// and (b) someone at our high school.
De Morganβs Laws
De Morganβs Laws were developed by Augustus De Morgan in the 1800s. They show how to simplify the negation of a complex boolean expression, which is when there are multiple expressions joined by an and (&&
) or or (||
), such as (x < 3) && (y > 2)
. When you negate one of these complex expressions, you can simplify it by flipping the operators and end up with an equivalent expression. De Morganβs Laws state the following equivalencies. Hereβs an easy way to remember De Morganβs Laws: move the NOT inside, AND becomes OR and move the NOT inside, OR becomes AND.
In Java, De Morganβs Laws are written with the following operators:
!(a && b)
is equivalent to!a || !b
!(a || b)
is equivalent to!a && !b
Going back to our example above, not(a senior AND at our high school)
is equivalent to not(a senior) OR not(at our high school)
using De Morganβs Laws:
a = "senior"
b = "at our high school"
!(a && b) is equivalent to !a || !b
You can also simplify negated boolean expressions that have relational operators like <
, >
, ==
. You can move the negation inside the parentheses by flipping the relational operator to its opposite sign. For example, not (c equals d) is the same as saying c does not equal d. An easy way to remember this is To move the NOT, flip the sign. Notice that ==
becomes !=
, but <
becomes >=
, >
becomes <=
, <=
becomes >
, and >=
becomes <
where the sign is flipped and an equal sign may also be added or removed.
!(c == d)
is equivalent toc != d
!(c != d)
is equivalent toc == d
!(c < d)
is equivalent toc >= d
!(c > d)
is equivalent toc <= d
!(c <= d)
is equivalent toc > d
!(c >= d)
is equivalent toc < d
Truth Tables
Although you do not have to memorize De Morganβs Laws for the CSA Exam, you should be able to show that two boolean expressions are equivalent. One way to do this is by using truth tables.
For example, we can show that !(a && b)
is equivalent to !a || !b
by constructing the truth table below and seeing that they give identical results for the 2 expressions (the last 2 columns in the table below are identical!).
a | b | !(a && b) | !a \|\| !b |
---|---|---|---|
true | true | false | false |
false | true | true | true |
true | false | true | true |
false | false | true | true |
Simplifying Boolean Expressions
Often, you can simplify boolean expressions to create equivalent expressions. For example, applying De Morganβs Laws to !(x < 3 && y > 2)
yields !(x < 3) || !(y > 2)
as seen in the figure below. This can then be simplified further by flipping the relational operators to remove the not. So, !(x < 3) || !(y > 2)
is simplified to (x >= 3 || y <= 2)
where the relational operators are flipped and the negation is removed. These two simplification steps are seen below.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.