Professional Documents
Culture Documents
Operations
on
Bits
Brooks/Cole,
2003
OBJECTIVES
After reading this chapter, the reader should
be able to:
Apply arithmetic operations on bits when the integer is
represented in twos complement.
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.
Brooks/Cole,
2003
Figure 4-1
Operations on bits
Brooks/Cole,
2003
4.1
ARITHMETIC
OPERATIONS
Brooks/Cole,
2003
Number of 1s
-----------None
One
Two
Three
Result
Carry
------------ -----------0
1
0
1
1
1
Brooks/Cole,
2003
Note:
Brooks/Cole,
2003
Example 1
Add two numbers in twos complement
representation:(+17)+(+22)(+39)
Solution
Carry
1
0 0 0 1 0 0
0 0 0 1 0 1
Result
0
1
1
0
---------------------------------0 0 1 0 0 1 1 1
39
Brooks/Cole,
2003
Example 2
Add two numbers in twos complement
representation:(+24)+(17)(+7)
Solution
Carry
1 1 1 1 1
0 0 0 1 1 0
1 1 1 0 1 1
Result
0
1
0
1
---------------------------------0 0 0 0 0 1 1 1
+7
Brooks/Cole,
2003
Example 3
Add two numbers in twos complement
representation:(35)+(+20)(15)
Solution
Carry
1 1 1
1 1 0 1 1 1
0 0 0 1 0 1
Result
0
0
1
0
---------------------------------1 1 1 1 0 0 0 1
-15
Brooks/Cole,
2003
Example 4
Add two numbers in twos complement
representation:(+127)+(+3)(+130)
Solution
Carry
Result
1 1 1 1 1 1
0 1 1 1 1 1
0 0 0 0 0 0
1
1
1
1
Note:
Range of numbers in twos
complement
representation
- (2N-1) ---------- 0 ----------+(2N-1 1)
Brooks/Cole,
2003
Figure 4-2
Brooks/Cole,
2003
Note:
Whenyoudoarithmeticoperationson
numbersinacomputer,rememberthat
eachnumberandtheresultshouldbe
intherangedefinedbythebitallocation.
Brooks/Cole,
2003
Example 5
Subtract 62 from 101 in twos complement:
(+101)(+62)(+101)+(62)
Solution
Carry
1 1
0 1 1 0 0 1
1 1 0 0 0 0
0
1
1
0
---------------------------------Result
0 0 1 0 0 1 1 1
The leftmost carry is discarded.
39
Brooks/Cole,
2003
Example 6
Addtwofloats:
01000010010110000000000000000000
01000001001100000000000000000000
Solution
Theexponentsare5and3.Thenumbersare:
+25x1.1011and+23x1.011
Maketheexponentsthesame.
(+25x1.1011)+(+25x0.01011) +25x10.00001
Afternormalization+26x1.000001,whichisstoredas:
010000101000001000000000000000000
Brooks/Cole,
2003
4.2
LOGICAL
OPERATIONS
Brooks/Cole,
2003
Figure 4-3
Brooks/Cole,
2003
Figure 4-4
Logical operations
Brooks/Cole,
2003
Figure 4-5
Truth tables
Brooks/Cole,
2003
Figure 4-6
NOT operator
Brooks/Cole,
2003
Example 7
UsetheNOToperatoronthebitpattern10011000
Solution
Target
10011000NOT
Result01100111
Brooks/Cole,
2003
Figure 4-7
AND operator
Brooks/Cole,
2003
Example 8
UsetheANDoperatoronbitpatterns10011000
and00110101.
Solution
Target
10011000AND
00110101
Result00010000
Brooks/Cole,
2003
Figure 4-8
Brooks/Cole,
2003
Figure 4-9
OR operator
Brooks/Cole,
2003
Example 9
UsetheORoperatoronbitpatterns10011000and
00110101
Solution
Target
10011000OR
00110101
Result10111101
Brooks/Cole,
2003
Figure 4-10
Brooks/Cole,
2003
Figure 4-11
XOR operator
Brooks/Cole,
2003
Example 10
UsetheXORoperatoronbitpatterns10011000
and00110101.
Solution
Target
10011000XOR
00110101
Result10101101
Brooks/Cole,
2003
Figure 4-12
Brooks/Cole,
2003
Figure 4-13
Mask
Brooks/Cole,
2003
Figure 4-14
Brooks/Cole,
2003
Example 11
Useamasktounset(clear)the5leftmostbitsofa
pattern.Testthemaskwiththepattern10100110.
Solution
Themaskis00000111.
Target
10100110AND
Mask00000111
Result00000110
Brooks/Cole,
2003
Example 12
Imagineapowerplantthatpumpswatertoacity
usingeightpumps.Thestateofthepumps(onor
off)canberepresentedbyan8bitpattern.For
example,thepattern11000111showsthatpumps
1to3(fromtheright),7and8areonwhile
pumps4,5,and6areoff.Nowassumepump7
shutsdown.Howcanamaskshowthissituation?
Solution
Usethemask10111111toANDwiththetarget
pattern.Theonly0bit(bit7)inthemaskturns
offtheseventhbitinthetarget.
Target
11000111AND
Mask10111111
Result10000111
Brooks/Cole,
2003
Figure 4-15
Brooks/Cole,
2003
Example 13
Useamasktosetthe5leftmostbitsofapattern.
Testthemaskwiththepattern10100110.
Solution
Themaskis11111000.
Target
10100110OR
Mask11111000
Result11111110
Brooks/Cole,
2003
Example 14
Usingthepowerplantexample,howcanyouuse
amasktotoshowthatpump6isnowturnedon?
Solution
Usethemask00100000.
Target
10000111OR
Mask00100000
Result10100111
Brooks/Cole,
2003
Figure 4-16
Brooks/Cole,
2003
Example 15
Useamasktoflipthe5leftmostbitsofapattern.
Testthemaskwiththepattern10100110.
Solution
Target
10100110XOR
Mask11111000
Result01011110
Brooks/Cole,
2003
4.3
SHIFT
OPERATIONS
Brooks/Cole,
2003
Figure 4-17
Shift operations
Brooks/Cole,
2003
Example 16
Showhowyoucandivideormultiplyanumber
by
2usingshiftoperations.
Solution
Ifabitpatternrepresentsanunsigned
number,arightshiftoperationdividesthe
numberbytwo.Thepattern00111011
represents59.Whenyoushiftthenumberto
theright,youget00011101,whichis29.Ifyou
shifttheoriginalnumbertotheleft,youget
01110110,whichis118.
Brooks/Cole,
2003
Example 17
Useacombinationoflogicalandshiftoperations
tofindthevalue(0or1)ofthefourthbit(from
theright).
Solution
Usethemask00001000toANDwiththetargetto
keepthefourthbitandcleartherestofthebits.
Continued on the next slide
Brooks/Cole,
2003
Solution (continued)
Target
abcdefghAND
Mask00001000
Result0000e000
Shiftthenewpatternthreetimestotheright
0000e000 00000e00 000000e0 0000000e
Nowitiseasytotestthevalueofthenewpatternas
anunsignedinteger.Ifthevalueis1,theoriginalbit
was1;otherwisetheoriginalbitwas0.
Brooks/Cole,
2003