There are four primary logical operations we'll do with hexadecimal and binary numbers: and
, or
, xor
(exclusive-or), and not
. Unlike for the arithmetic operations, a hexadecimal calculator isn't necessary to perform these operations. It is often easier to do them by hand than to use an electronic device to compute them. The logical and
operation is a dyadic[22] operation (meaning it accepts exactly two operands). These operands are individual binary bits. The and
operation is:
0 and 0 = 0 0 and 1 = 0 1 and 0 = 0 1 and 1 = 1
A compact way to represent the logical and
operation is with a truth table. A truth table takes the form shown in Table 2-2.
This is just like the multiplication tables you've encountered in school. The values in the left column correspond to the leftmost operand of the and
operation. The values in the top row correspond to the rightmost operand of the and
operation. The value located at the intersection of the row and column (for a particular pair of input values) is the result of logically and
ing those two values together.
In English, the logical and
operation is, "If the first operand is 1 and the second operand is 1, the result is 1; otherwise the result is 0." We could also state this as, "If either or both operands are 0, the result is 0."
One important fact to note about the logical and
operation is that you can use it to force a 0 result. If one of the operands is 0, the result is always 0 regardless of the other operand. In the truth table above, for example, the row labeled with a 0 input contains only 0s, and the column labeled with a 0 contains only 0 results. Conversely, if one operand contains a 1, the result is exactly the value of the second operand. These results of the and
operation are very important, particularly when we want to force bits to 0. We will investigate these uses of the logical and
operation in the next section.
The logical or
operation is also a dyadic operation. Its definition is:
0 or 0 = 0 0 or 1 = 1 1 or 0 = 1 1 or 1 = 1
The truth table for the or
operation takes the form appearing in Table 2-3.
Colloquially, the logical or
operation is, "If the first operand or the second operand (or both) is 1, the result is 1; otherwise the result is 0." This is also known as the inclusive-or operation.
If one of the operands to the logical or
operation is a 1, the result is always 1 regardless of the second operand's value. If one operand is 0, the result is always the value of the second operand. Like the logical and
operation, this is an important side effect of the logical or
operation that will prove quite useful.
Note that there is a difference between this form of the inclusive logical or
operation and the standard English meaning. Consider the phrase "I am going to the store or I am going to the park." Such a statement implies that the speaker is going to the store or to the park but not to both places. Therefore, the English version of logical or
is slightly different from the inclusive-or operation; indeed, this is the definition of the exclusive-or operation.
The logical xor
(exclusive-or) operation is also a dyadic operation. Its definition follows:
0 xor 0 = 0 0 xor 1 = 1 1 xor 0 = 1 1 xor 1 = 0
The truth table for the xor
operation takes the form shown in Table 2-4.
In English, the logical xor
operation is, "If the first operand or the second operand, but not both, is 1, the result is 1; otherwise the result is 0." Note that the exclusive-or operation is closer to the English meaning of the word or than is the logical or
operation.
If one of the operands to the logical exclusive-or operation is a 1, the result is always the inverse of the other operand; that is, if one operand is 1, the result is 0 if the other operand is 1, and the result is 1 if the other operand is 0. If the first operand contains a 0, then the result is exactly the value of the second operand. This feature lets you selectively invert bits in a bit string.
The logical not
operation is a monadic operation (meaning it accepts only one operand):
not 0 = 1 not 1 = 0
The truth table for the not
operation appears in Table 2-5.
[22] Many texts call this a binary operation. The term dyadic means the same thing and avoids the confusion with the binary numbering system.