Boolean logic contains four operators that perform various actions in a Boolean logic statement. Before we learn about them, let’s take a minute to discuss variables in Boolean logic.
As we covered in a previous chapter, most programming languages support a special data type for storing Boolean values. That data type can only contain one of two values, True
or False
. So, in a Boolean logic statement, any variable can only be either True
or False
For each of the operators below, we’ll see both a truth table and Venn diagram representation of the operation. They both present the same information, but in a slightly different way. In each operation, we’ll see all possible values of the variables present in the statement, and the resulting value of the operation on those variables. Since each variable can only have two values, the possibilities are limited enough that we can show all of them.
In the Venn diagrams below, each circle represents a variable.1 The variables are labeled in the circles on each diagram.
Not
The first, and simplest, Boolean logic operator is the not, or negation, operator. This operator simply negates the given Boolean value, turning True
into False
and False
into True
. When written, we use the
!
instead.
Below is a truth table giving the value of
|
|
|
|
---|---|---|---|
F | F | T | T |
F | T | T | F |
T | F | F | T |
T | T | F | F |
We can also see the operation visually using a Venn diagram. In these drawings, assume each individual variable is True
, and the shaded portion shows which parts of the diagram are True
for the entire statement.
As we can see, if
We can also extend this to three variables, as shown in the truth table below. It gives the values of
|
|
|
|
|
|
---|---|---|---|---|---|
F | F | F | T | T | T |
F | F | T | T | T | F |
F | T | F | T | F | T |
F | T | T | T | F | F |
T | F | F | F | T | T |
T | F | T | F | T | F |
T | T | F | F | F | T |
T | T | T | F | F | F |
The Venn diagram for this instance is very similar:
As we can see, the not operation is very useful when dealing with Boolean values.
And
The next Boolean logic operator we’ll cover is the and, or conjunction, operator. This operator works similar to the way we use “and” in our spoken language. If
True
, while
True
, then we can say that both
True
. This would be written as
&&
to represent the Boolean and operator, but some languages also include and
as a keyword to denote this operator.
Here is a truth table giving the value of
|
|
|
---|---|---|
F | F | F |
F | T | F |
T | F | F |
T | T | T |
As we can see, the only time that
True
is when both
True
.
Next, we can look at a Venn Diagram that represents this operation
As we can see, only the center of the Venn diagram is shaded, representing the parts of the diagram that are both in
This operation also easily extends to three variables. This truth table gives the value of
|
|
|
|
---|---|---|---|
F | F | F | F |
F | F | T | F |
F | T | F | F |
F | T | T | F |
T | F | F | F |
T | F | T | F |
T | T | F | F |
T | T | T | T |
The Venn diagram for this instance is very similar to the one above:
Once again, we see that only the very center of the diagram is shaded, as expected.
Or
The third Boolean operator is the or, or disjunction, operator. It is somewhat similar to how we use the word “or” in our spoken language, with a major difference. This operator would be written as
||
to represent the Boolean or operator, but some languages also include or
as a keyword to denote this operator.
Consider ordering food at a restaurant as an example. On the menu, you might see the statement “Soup or Salad” as one of the options for your meal. This means that you must choose either “soup” or “salad” to go with your meal, but usually you aren’t allowed to have both (at least, without paying more).
Now, consider a similar statement,
True
, we would say that
True
. However, if both
True
, we would also say that
True
. So, the Boolean or operator will return True
when both inputs are True
, which differs from how we use it in spoken language.
Here is a truth table giving the value of
|
|
|
---|---|---|
F | F | F |
F | T | T |
T | F | T |
T | T | T |
As we can see, the only time that
False
is when both
False
. As long as at least one of the values is True
, the whole statement is True
.
Next, we can look at a Venn Diagram that represents this operation
As we can see, both circles are completely shaded, showing that
This operation also easily extends to three variables. This truth table gives the value of
|
|
|
|
---|---|---|---|
F | F | F | F |
F | F | T | T |
F | T | F | T |
F | T | T | T |
T | F | F | T |
T | F | T | T |
T | T | F | T |
T | T | T | T |
The Venn diagram for this instance is very similar to the one above:
Once again, we see that all parts of the Venn diagram are shaded, except for the part outside of all the circles.
Exclusive Or
The last Boolean operator we’ll cover is exclusive or, or exclusive disjunction. We’ll sometimes see this referred to as xor as well. When written, we use the
This operator works in the same way that “or” works in our spoken language. Recall the “Soup or Salad” example from earlier. The exclusive or operation is True
if only one of the inputs is True
. If both inputs are True
, the output is False
.
Here is a truth table giving the value of
|
|
|
---|---|---|
F | F | F |
F | T | T |
T | F | T |
T | T | F |
As we can see, the result is True
when either
True
, but it is False
if both of them are True
, or if both of them are False
.
As a Venn diagram, it would look like this:
As we can see, the parts of the diagram in
This operation is most interesting when extended to three variables. This truth table gives the value of
|
|
|
|
---|---|---|---|
F | F | F | F |
F | F | T | T |
F | T | F | T |
F | T | T | F |
T | F | F | T |
T | F | T | F |
T | T | F | F |
T | T | T | T |
The Venn diagram for this instance is as follows:
In this instance, we see the parts of the diagram representing items that are just in
Just like we hopefully remember from mathematics, when we have multiple operations chained together, we must respect the order of operations. So, we can insert parentheses into the original statement to show how it would actually be calculated:
Now, let’s assume that all three of our variables are True
, represented by
As we can see, since
True
, we find that
False
, as expected. Now, we can continue to reduce the expression:
In this case, since only one of the two operands is True
, the result of exclusive or would also be True
. Therefore,
True
when all three variables are True
. In short, a string of exclusive ors is True
if an odd number of the variables are True
, and it is False
if an even number of variables are True
.
Not all languages support an logical exclusive or. Alternate forms are
At some restaurants, you are given a choice of not just two, but three items to go with your meal. Of course, the intent is for you to choose just one of the three items listed.
However, now that you understand how exclusive or works, you might see how you could get all three items instead of just one! Logic is pretty powerful, isn’t it?
Comparators
Finally, it is worth discussing other operators that can result in a Boolean value. The most important of these operators are the comparators, which compare two values and return a result that is either True
or False
. We’ve definitely seen these operators before in mathematics, but may not have actually realized that they result in a Boolean value.
Below is a table giving the common comparators used in programming, along with an example of how they might be used:
Name | Operator | Symbol | True Example |
False Example |
---|---|---|---|---|
Equal |
|
== |
1 == 1 |
1 == 2 |
Not Equal |
|
!= |
1 != 2 |
1 != 1 |
Greater Than |
|
> |
2 > 1 |
1 > 2 |
Greater Than or Equal To |
|
>= |
1 >= 1 |
1 >= 2 |
Less Than |
|
< |
1 < 2 |
2 < 1 |
Less Than or Equal To |
|
<= |
1 <= 1 |
2 <= 1 |
That covers all of the basic operators and symbols in Boolean logic. On the next page, we’ll learn how to combine them together and create logical statements using the rules of Boolean algebra.
-
The Venn diagrams are adapted from ones found on Wikimedia Commons ↩︎