0% found this document useful (0 votes)
9 views7 pages

Lecture - 05 Operations On Bits Ch4.2

Chapter 4 reviews two's complement representation, logical operations on bits, and shift operations. It explains how to perform various logical operations such as AND, OR, NOT, and XOR, as well as how to use masks to manipulate specific bits in a pattern. Additionally, it covers the effects of shifting bits for multiplication and division by powers of two.

Uploaded by

d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Lecture - 05 Operations On Bits Ch4.2

Chapter 4 reviews two's complement representation, logical operations on bits, and shift operations. It explains how to perform various logical operations such as AND, OR, NOT, and XOR, as well as how to use masks to manipulate specific bits in a pattern. Additionally, it covers the effects of shifting bits for multiplication and division by powers of two.

Uploaded by

d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 4 Two’s Complement :: Review

Operations In two’s complement representation,


on the leftmost bit defines the sign of the
number. If it is 0, the number is positive.
If it is 1, the number is negative.
Bits

Figure 4-1

Two’s Complement :: Review Operations on bits :: Review

Two’s complement can be achieved by


reversing all bits except the rightmost bits up to
the first 1 (inclusive). If you two’s complement
a positive number, you get the corresponding
negative number. If you two’s complement a
negative number, you get the corresponding
positive number. If you two’s complement a
number twice, you get the original number.

OBJECTIVES Reading

• Chapter 4
Apply logical operations on bits.

Understand the applications of logical operations using masks.

Understand the shift operations on numbers and how a number


can be multiplied or divided by powers of two using shift
operations.

1
Logical Operations

• A single bit can be either 0 or 1


4.2 • We can interpret 0 and 1 logically
• 0 – false;
LOGICAL • 1 – true;
OPERATIONS • Logical operation – operations applied on
bits interpreted as logical values

Figure 4-3 Figure 4-4


Unary and binary operations Logical operations

Figure 4-5 Figure 4-6


Truth tables NOT operator

2
Figure 4-7
Example 7 AND operator
Use the NOT operator on the bit pattern 10011000

Solution
Target 10011000 NOT
------------------
Result 01100111

Figure 4-8
Example 8 Inherent rule of the AND operator
Use the AND operator on bit patterns 10011000
and 00110101.

Solution
Target 10011000 AND
00110101
------------------
Result 00010000

Figure 4-9
OR operator Example 9
Use the OR operator on bit patterns 10011000 and
00110101

Solution
Target 10011000 OR
00110101
------------------
Result 10111101

3
Figure 4-10 Figure 4-11
Inherent rule of the OR operator XOR operator

Figure 4-12
Example 10 Inherent rule of the XOR operator
Use the XOR operator on bit patterns 10011000
and 00110101.

Solution
Target 10011000 XOR
00110101
------------------
Result 10101101

Figure 4-13 Figure 4-14


Mask Example of unsetting specific bits

4
Example 11 Example 12
Use a mask to unset (clear) the 5 leftmost bits of a Imagine a power plant that pumps water to a city
pattern. Test the mask with the pattern 10100110. using eight pumps. The state of the pumps (on or
off) can be represented by an 8-bit pattern. For
Solution example, the pattern 11000111 shows that pumps
1 to 3 (from the right), 7 and 8 are on while pumps
The mask is 00000111.
00000111. 4, 5, and 6 are off. Now assume pump 7 shuts
down. How can a mask show this situation?
Target 10100110 AND
Mask 00000111
------------------ Solution on the next slide.
Result 00000110

Figure 4-15

Solution Example of setting specific bits

Use the mask 1010111111 to AND with the target


pattern. The only 0 bit (bit 7) in the mask turns
off the seventh bit in the target.
Target 11000111 AND
Mask 10111111
------------------
Result 10000111

Example 13 Example 14
Use a mask to set the 5 leftmost bits of a pattern. Using the power plant example, how can you use
Test the mask with the pattern 10100110. a mask to to show that pump 6 is now turned on?
Solution Solution
The mask is 11111000.
11111000.
Use the mask 001
00100000.
00000.
Target 10100110 OR
Mask 11111000 Target 10000111 OR
------------------ Mask 00100000
Result 11111110 ------------------
Result 10100111

5
Figure 4-16
Example of flipping specific bits Example 15
Use a mask to flip the 5 leftmost bits of a pattern.
Test the mask with the pattern 10100110.

Solution
Target 10100110 XOR
Mask 11111000
------------------
Result 01011110

Figure 4-17
Shift operations

4.3
SHIFT
OPERATIONS

Example 16 Example 17
Use a combination of logical and shift operations
Show how you can divide or multiply a number by
to find the value (0 or 1) of the fourth bit (from the
2 using shift operations.
right).
Solution
If a bit pattern represents an unsigned
number, a right-
right-shift operation divides the Solution
number by two. The pattern 00111011 Use the mask 00001000 to AND with the target to
represents 59. When you shift the number to keep the fourth bit and clear the rest of the bits.
the right, you get 00011101, which is 29. If you
shift the original number to the left, you get Continued on the next slide
01110110, which is 118.

6
Solution (continued)
Summary
Target abcd efgh AND
Mask 00001000 • You can perform arithmetic and logical
------------------
operations on bits
Result 0000e 000
• Logical operations on bits can be unary or
Shift the new pattern three times to the right
binary
0000e000 Î 00000e
0000e 00000e00 Î 000000e
000000e0 Î 0000000e
0000000e • The unary NOT operator inverts its input
• The result of the binary AND operation is true
Now it is easy to test the value of the new pattern as only if both inputs are true
an unsigned integer. If the value is 1, the original bit
was 1; otherwise the original bit was 0.

Summary Summary

• The result of the binary OR operation is false • To set a bit in a target bit pattern, set the
only if both inputs are false corresponding mask bit to 1 and use OR
• The result of the binary XOR operation is false • To flip a bit in a target bit pattern, set the
only if both inputs are the same corresponding mask bit to 1 and use the XOR
• A mask is a bit pattern that is applied to a operator
target bit pattern to achieve a specific result
• To clear a bit in a target bit pattern, set the
corresponding mask bit to 0 and use AND

You might also like