Computer Arithmetic Algorithms Prof. Dae Hyun Kim School of Electrical Engineering and Computer Science Washington State University Number Representations
Information Textbook Israel Koren, Computer Arithmetic Algorithms, Prentice Hall, 1993. 2
Outline Binary number system Radix conversion Negative numbers Signed-magnitude One s complement Two s complement Addition and subtraction Arithmetic shift operations (sign extension) 3
The Binary Number System A binary number of length nn (xx nn 1, xx nn 2,, xx 1, xx 0 ) xx ii : bit ( {0,1}) An nn-digit binary number nn 1 XX = xx nn 1 2 nn 1 + xx nn 2 2 nn 2 + + xx 1 2 1 + xx 0 2 0 = xx ii 2 ii xx ii {0,1} ii=0 radix Example 1011 2 = 1 2 3 + 0 2 2 + 1 2 1 + 1 2 0 = 11 4
The Binary Number System Radix-10 numbers: decimal numbers (101) 10 = 101 Radix-2 numbers: binary numbers (101) 2 = 5 Range of representable numbers [XX mmmmmm, XX mmmmmm ] Example: The range of 4-bit unsigned binary numbers: [0, 15] Overflow An arithmetic operation resulting in a number larger than XX mmmmmm or smaller than XX mmmmmm 5
The Binary Number System The conventional number systems Nonredundant Every number has a unique representation. Weighted nn 1 XX = ii=0 xx ii ww ii ww ii : weight Positional ww ii depends only on the position of the digit xx ii. ww ii = rr ii 0 xx ii rr 1 Otherwise (if xx ii rr), the system becomes redundant. X xx ii xx kk redundant D dd jj 6
The Binary Number System A mixed number (fixed-point representation) (xx kk 1 xx kk 2 xx 1 xx 0. xx 1 xx 2 xx mm ) rr integral part fractional part = (xx kk 1 rr kk 1 + xx kk 2 rr kk 2 + + xx 1 rr 1 + xx 0 rr 0 ) + (xx 1 rr 1 + + xx mm rr mm ) kk 1 = xx ii rr ii ii= mm rr mm : indicates the position of the radix point. ulp: unit in the last position uuuuuu = rr mm 7
Radix Conversion Given XX = XX II + XX FF = kk 1 ii=0 xx ii rr ii + 1 ii= mm xx ii rr ii XX II : integral part XX FF : fractional part rr DD : destination number system Integral part XX II = xx 0 + rr DD (xx 1 + rr DD (xx 2 + + rr DD (xx kk 2 + rr DD xx kk 1 ))) 0 xx ii < rr DD xx 0 = XX II mmmmmm rr DD qq 0 = xx 1 + rr DD xx 2 + rr DD xx 3 + xx 1 = qq 0 mmmmmm rr DD... 8
Radix Conversion Fractional part XX FF = rr DD 1 (xx 1 + rr DD 1 (xx 2 + + rr DD 1 (xx mm 1 + rr DD 1 xx mm ))) 0 xx ii < rr DD xx 1 : the integral part of XX FF rr DD mm 1 = rr DD 1 (xx 2 + + rr DD 1 (xx mm 1 + rr DD 1 xx mm )) xx 2 : the integral part of mm 1 rr DD... Example 46.375 10 = 101110.011 2 9
Negative Numbers: Signed-Magnitude Representation (xx ss xx nn 2 xx nn 3 xx 1 xx 0 ) sign magnitude Positive numbers: 0xxxxx 0xxxxx 2 Negative numbers: (r-1)xxxxx 1xxxxx 2 Range of positive numbers for (ss xx kk 2 xx kk 3 xx 1 xx 0. xx 1 xx 2 xx mm ) [0, rr kk 1 uuuuuu] Range of negative numbers for (ss xx kk 2 xx kk 3 xx 1 xx 0. xx 1 xx 2 xx mm ) [ (rr kk 1 uuuuuu), 0] 10
Negative Numbers: Signed-Magnitude Problems Two zeros 0000...0 1000...0 Difficulties in subtraction If X>Y, X-Y is ok. However, Y-X is -(X-Y), so we should compute X-Y, then set the MSB to 1. 11
Negative Numbers: Complement Represent Y by R Y. Then, ( Y) = R (R Y) = Y. If X>Y>0, Y X = Y + (R X) = R (X Y) = X Y. Two requirements When X>Y>0, X Y = X + (R Y) = R + (X Y) should be X Y. Computation of R Y should be simple. Notation xx ii = rr 1 xx ii XX = (xx kk 1, xx kk 2,, xx 0, xx 1,, xx mm ) XX + XX = ( rr 1, rr 1,, (rr 1)) XX + XX + uuuuuu = rr kk 12
Negative Numbers: Complement One s complement (Diminished-radix complement) RR = rr kk uuuuuu XX = RR XX = XX When X>Y>0, YY XX = YY + RR XX = RR XX YY = (YY XX) When X>Y>0, XX YY = XX + RR YY = RR + XX YY = rr kk + XX YY uuuuuu = XX YY uuuuuu This requires a correction step (add ulp). When X>0, Y>0, XX YY = RR XX + RR YY = RR + RR XX + YY = rr kk + RR XX + YY = RR XX + YY = (XX + YY) Two s complement (Radix complement) RR = rr kk XX = RR XX = XX + uuuuuu When X>Y>0, YY XX = YY + RR XX = RR YY XX = (YY XX) When X>Y>0, XX YY = XX + RR YY = RR + XX YY = rr kk + XX YY = XX YY When X>0, Y>0, XX YY = RR XX + RR YY = RR + RR XX + YY = rr kk + RR XX + YY = RR XX + YY = (XX + YY) 13
Two s Complement Representation One zero 000...0 A negative number that does not have a positive equivalent 100...0 Range [ 2 nn 1, 2 nn 1 uuuuuu] XX = xx nn 1 2 nn 1 + nn 2 ii=0 xx ii 2 ii Proof) If xx nn 1 = 0 (positive), XX = nn 2 ii=0 xx ii 2 ii = xx nn 1 2 nn 1 + nn 2 xx ii 2 ii If xx nn 1 = 1 (negative), XX = XX + uuuuuu = [ nn 1 ii=0 xx ii 2 ii + 1] = ii=0 nn 1 1 xx ii 2 ii + 1 = 2 nn 1 nn 1 ii=0 xx ii 2 ii + 1 nn 1 nn 2 = 2 nn + ii=0 xx ii 2 ii = 2 nn 1 + ii=0 xx ii 2 ii ii=0. 14
One s Complement Representation Two zeros 000...0 111...1 Range [ 2 nn 1 uuuuuu, 2 nn 1 uuuuuu ] XX = xx nn 1 2 nn 1 uuuuuu + nn 2 xx ii 2 ii ii=0 Proof) If xx nn 1 = 0 (positive), XX = ii=0 xx ii 2 ii = xx nn 1 (2 nn 1 uuuuuu) + nn 2 xx ii 2 ii ii=0. nn 2 nn 1 nn 1 If xx nn 1 = 1 (negative), XX = XX = ii=0 xx ii 2 ii = ii=0 1 xx ii 2 ii = 2 nn 1 nn 1 ii=0 xx ii 2 ii = 2 nn + 1 + nn 1 ii=0 xx ii 2 ii = 2 nn 1 + 1 + nn 2 ii=0 xx ii 2 ii = xx nn 1 (2 nn 1 uuuuuu) + ii=0 nn 2 xx ii 2 ii 15
Addition and Subtraction Signed-magnitude 0 1 0 1 1 (11) + 0 0 0 1 0 (2) = 0 1 1 0 1 (13) 0 1 0 1 1 (11) + 0 0 1 1 0 (6) = 1 0 0 0 1 (-1) 1 0 0 1 1 (-3) + 1 0 1 0 1 (-5) =1 0 1 0 0 0 (+8) 1 1 0 0 0 (-8) + 1 1 0 0 1 (-9) =1 1 0 0 0 1 (-1) overflow overflow overflow 0 1 1 0 1 (13) + 1 1 0 0 0 (-8) = 1 0 0 1 0 1 (5) 1 1 1 0 1 (-13) + 0 1 0 0 0 (8) = 1 0 0 1 0 1 (+5) As shown above, addition and subtraction of signed-magnitude numbers need corrections. 16
Addition and Subtraction Two s complement 0 1 0 1 1 (11) + 0 0 0 1 0 (2) = 0 1 1 0 1 (13) 0 1 0 1 1 (11) + 0 0 1 1 0 (6) = 1 0 0 0 1 (-15) 1 1 1 0 1 (-3) + 1 1 0 1 1 (-5) =1 1 1 0 0 0 (-8) 1 1 0 0 0 (-8) + 1 0 1 1 1 (-9) =1 0 1 1 1 1 (-17) overflow indicator overflow indicator 0 1 1 0 1 (13) + 1 1 0 0 0 (-8) = 1 0 0 1 0 1 (5) 1 0 0 1 1 (-13) + 0 1 0 0 0 (8) = 1 1 0 1 1 (-5) 17
Arithmetic Shift Operations For given {xx nn 1, xx nn 2,, xx 1, xx 0 } Signed-magnitude xx nn 1, 0, 0,, 0, xx nn 2,, xx 1, xx 0, 0, 0, Two s complement xx nn 1, xx nn 1,, xx nn 1, xx nn 1, xx nn 2,, xx 1, xx 0, 0, 0, One s complement xx nn 1, xx nn 1,, xx nn 1, xx nn 1, xx nn 2,, xx 1, xx 0, xx nn 1, xx nn 1, 18
Supplementary Materials
Arithmetic Shift Operations Derivation of the extension of the two s complement XX = XX nn 1 0 = {xx nn 1, xx nn 2,, xx 1, xx 0 } If X>0, it is the same as the signed-magnitude case. If X<0 (if X -2 n ), XX = XX nn 1 0 = xx nn 1, xx nn 2,, xx 1, xx 0 XX = {XX nn 1 0 + uuuuuu 0 } ( XX) ext = 00 0 XX nn 1 0 + uuuuuu 0 00 0 = 00 0 KK 00 0 XX ext = 00 0 KK 00 0 + uuuuuu mm = 11 1 KK 11 1 + uuuuuu mm = 11 1 KK + uuuuuu 0 00 0 = 11 1 2 nn KK 00 0 = 11 1 2 nn XX nn 1 0 + uuuuuu 0 00 0 = 11 1 XX nn 1 0 00 0 XX ext = xx nn 1 xx nn 1 xx nn 1, xx nn 2,, xx 1, xx 0 00 0 If X=2 n, we can directly prove it. 20
Arithmetic Shift Operations Derivation of the extension of the one s complement XX = XX nn 1 0 = {xx nn 1, xx nn 2,, xx 1, xx 0 } If X>0, it is the same as the signed-magnitude case. If X<0, XX = XX nn 1 0 = xx nn 1, xx nn 2,, xx 1, xx 0 XX = {XX nn 1 0 } ( XX) ext = 00 0 XX nn 1 0 00 0 XX ext = 00 0 XX nn 1 0 00 0 = 11 1 XX nn 1 0 11 1 XX ext = xx nn 1 xx nn 1 xx nn 1, xx nn 2,, xx 1, xx 0 xx nn 1 xx nn 1 21