Chapter 04 22 New
Chapter 04 22 New
Chapter 04 22 New
shift operations.
Logic Operation
& (AND)
Logic operations at bit level
• A bit can take one of the two values: 0 or 1.
• If we interpret 0 as the value false and 1 as the
value true, we can apply the operations defined in
Boolean algebra to manipulate bits.
• Boolean algebra, named in honor of George Boole,
belongs to a special field of mathematics called
logic.
Truth table
Exclusive OR
NOT
The NOT operator is a unary operator: it takes
only one input. The output bit is the complement
of the input.
For x = 0 NOT x → 1
x = 1 NOT x → 0
AND
The AND operator is a binary operator: it takes
two inputs. The output bit is 1 if both inputs are 1s
and the output is 0 in the other three cases.
For x = 0 or 1 x AND 0 → 0
0 AND x → 0
OR
The OR operator is a binary operator: it takes two
inputs. The output bit is 0 if both inputs are 0s and
the output is 1 in other three cases.
For x = 0 or 1 x OR 1 → 1
1 OR x → 1
XOR
The XOR operator is a binary operator like the OR
operator, with only one difference: the output is 0 if
both inputs are 1s.
For x = 0 or 1
1 XOR x → NOT x
x XOR 1 → NOT x
Other bit-level logic operations
x NAND y = NOT (x AND y)
x AND y = NOT (x NAND y)
x y x AND y x NAND y
0 0 0 1
0 1 0 1
1 0 0 1
1 1 1 0
NOT(x AND y)
NOT(x NAND y)
Other bit-level logic operations
x NOR y = NOT (x OR y)
x OR y = NOT (x NOR y)
x y x OR y x NOR y
0 0 0 1
0 1 1 0
1 0 1 0
1 1 1 0
NOT(x OR y)
NOT(x NOR y)
Other bit-level logic operations
x XNOR y = NOT (x XOR y)
x XOR y = NOT (x XNOR y)
x y x XOR y x XNOR y
0 0 0 1
0 1 1 0
1 0 1 0
1 1 0 1
NOT(x XOR y)
NOT(x XNOR y)
Example 4.1
In English we use the conjunction “or” sometimes to
means an inclusive-or, and sometimes to means an
exclusive-or.
a. The sentence “I wish to have a car or a house” uses
“or” in the inclusive sense—I wish a car, a house, or
both.
0 0 0 0 0
0 1 1 1 0
1 0 1 0 1
1 1 0 0 0
Exercises
• The same four operators (NOT, AND, OR, and XOR) can be applied to an n-bit
pattern.
• The effect is the same as applying each operator to each individual bit for NOT
Solution
The solution is shown below. Note that the NOT operator
changes every 0 to 1 and every 1 to 0.
Solution
The solution is shown below. Note that only one bit in the
output is 0, where both corresponding inputs are 0s.
Example 4.6
Use the XOR operator on the bit patterns 10011001 and
00101110.
Solution
The solution is shown below. Compare the output in this
example with the one in Example 4.5. The only difference
is that when the two inputs are 1s, the result is 0 (the
effect of exclusion).
Applications
Solution
The mask is 00000111. The result of applying the mask is:
Example 4.8
Solution
The mask is 11111000. The result of applying the mask is:
Example 4.9
4.24
4.2 SHIFT OPERATIONS
• Shift operations move the bits in a pattern, changing
the positions of the bits. They can move bits to the
left or to the right.
• We can classify shift operations into two categories:
logical shift operations and arithmetic shift
operations.
Logical shift operations
• A logical shift operation is applied to a
pattern that does not represent a signed number.
• The reason is that these shift operations may
change the sign of the number that is defined by
the leftmost bit in the pattern.
• We distinguish two types of logical shift
operations, as described below:
Logical shift
Solution
The solution is shown below. The leftmost bit is lost and a
0 is inserted as the rightmost bit.
Discarded
Added
Figure 4.4: Circular shift operations
Example 4.11
Solution
The solution is shown below. The leftmost bit is circulated
and becomes the rightmost bit.
4.2.2 Arithmetic Shift Operations
• Arithmetic shift operations assume that the bit
pattern is a signed integer in two’s complement
format.
• Arithmetic right shift is used to divide an integer by
two, while arithmetic left shift is used to multiply an
integer by two.
Figure 4.5 Arithmetic shift operations
0
Lost c. Yet another Arithmetic left shift
Example 4.12
Use an arithmetic right shift operation on the bit pattern
10011001. The pattern is an integer in two’s complement
format. [-128~127]
Solution
The solution is shown below. The leftmost bit is retained
and also copied to its right neighbor bit.
Solution
The solution is shown below. The leftmost bit is lost and a
0 is inserted as the rightmost bit.
This bit must be one without underflow
The original number was −39 and the new number is −78.
The original number is multiplied by two. The operation is
valid because no underflow occurred.
Example 4.14
Use an arithmetic left shift operation on the bit pattern
01111111. The pattern is an integer in two’s complement
format.
Solution
The solution is shown below. The leftmost bit is lost and a
0 is inserted as the rightmost bit.
This bit must be 0 without overflow at
The original number was 127 and the new number is −2.
Here the result is not valid because an overflow has
occurred. The expected answer 127 × 2 = 254 cannot be
represented by an 8-bit pattern.
Exercise
4.36
Example 4.15 Combining shift and logic operations
In other words:
A − B ↔ A + (B + 1)
where B is the ones’ complement of B and
(B + 1) means the two’s complement of B
Addition of two integers
carry
sum
carry
sum
Solution
The operation is adding. A is added to B and the result is
stored in R. (+17) + (+22) = (+39).
Example 4.17
Solution
The operation is adding. A is added to B and the result is
stored in R. (+24) + (-17) = (+7).
carry out at
MSB position
Example 4.18
Solution
The operation is subtracting. A is added to (B + 1) and the
result is stored in R. (+24) - (-17) = (+41).
Example 4.19
Solution
The operation is adding. A is added to B and the result is stored in R.
If carries at these two positions are different, there is an
overflow.