2.7 Logical Operations on Bits


2.7 Logical Operations on Bits

There are four primary logical operations we'll do with hexadecimal and binary numbers: AND, OR, XOR (exclusive-or), and NOT. Unlike 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[3] 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.

Table 2-2: AND Truth Table

AND

0

1


0

0

0

1

0

1

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 ANDing those two values together.

In English, the logical AND operation is, "If the first operand is one and the second operand is one, the result is one; otherwise the result is zero." We could also state this as "If either or both operands are zero, the result is zero."

One important fact to note about the logical AND operation is that you can use it to force a zero result. If one of the operands is zero, the result is always zero regardless of the other operand. In the truth table above, for example, the row labeled with a zero input contains only zeros and the column labeled with a zero only contains zero results. Conversely, if one operand contains a one, 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 zero. 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.

Table 2-3: OR Truth Table

OR

0

1


0

0

1

1

1

1

Colloquially, the logical OR operation is, "If the first operand or the second operand (or both) is one, the result is one; otherwise the result is zero." This is also known as the inclusive-OR operation.

If one of the operands to the logical-OR operation is a one, the result is always one regardless of the second operand's value. If one operand is zero, 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 than 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.

Table 2-4: XOR Truth Table

XOR

0

1


0

0

1

1

1

0

In English, the logical XOR operation is, "If the first operand or the second operand, but not both, is one, the result is one; otherwise the result is zero." 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 one, the result is always the inverse of the other operand; that is, if one operand is one, the result is zero if the other operand is one and the result is one if the other operand is zero. If the first operand contains a zero, 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). It is:

                NOT 0 = 1                NOT 1 = 0 

The truth table for the NOT operation appears in Table 2-5.

Table 2-5: NOT Truth Table

NOT

0

1


1

0

[3]Many texts call this a binary operation. The term dyadic means the same thing and avoids the confusion with the binary numbering system.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net