3.2 Logical Operations on Bits


3.2 Logical Operations on Bits

There are four main logical operations we'll need to perform on 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.

The logical AND, OR, and XOR operations accept two single-bit operands and compute the following results:

 AND:               0 and 0 = 0               0 and 1 = 0               1 and 0 = 0               1 and 1 = 1  OR:               0 or 0 = 0               0 or 1 = 1               1 or 0 = 1               1 or 1 = 1  XOR:               0 xor 0 = 0               0 xor 1 = 1               1 xor 0 = 1               1 xor 1 = 0 

Table 3-1, Table 3-2, and Table 3-3 show the truth tables for the AND, OR, and XOR operations. A truth table is just like the multiplication tables you encountered in elementary school. The values in the left column correspond to the left operand of the operation. The values in the top row correspond to the right operand of the operation. The value located at the intersection of the row and column (for a particular pair of input values) is the result.

Table 3-1: AND truth table

AND

1

1

1

Table 3-2: OR truth table

OR

1

1

1

1

1

Table 3-3: XOR truth table

XOR

1

1

1

1

In plain English, the logical AND operation translates as, '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.' The logical AND operation is useful for forcing a zero result. If one of the operands is zero, the result is always zero regardless of the value of the other operand. If one of the operands contains one, then the result is the value of the other operand.

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 one, the result is always one. If an operand is zero, the result is always the value of the other operand.

In English, the logical XOR operation is, 'If the first or second operand, but not both, is one, the result is one; otherwise the result is zero.' If one of the operands is a one, the result is always the inverse of the other operand.

The logical NOT operation is unary (meaning it accepts only one operand). The truth table for the NOT operation appears in Table 3-4. This operator simply inverts (reverses) the value of its operand.

Table 3-4: NOT truth table

NOT

1

 

1




Write Great Code. Understanding the Machine, Vol. 1
The Art of Assembly Language
ISBN: 1593270038
EAN: 2147483647
Year: 2003
Pages: 144
Authors: Randall Hyde

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