Computer Architecture 10 Representation of Numbers Made with OpenOffice.org 1
Number encodings Additive systems - historical Positional systems radix - the base of the numbering system, the positive integer that is equivalent to 1 in the next higher counting place fixed radix radix constant, identical for at all positions of digits mixed radix radix vector of bases for all parts of number examples: time (0, 24, 60,60) angle - (0, 360, 60, 60) Digit sets of arbitrary size and composition together with arbitrary radix (negative, fractional, imaginary numbers, etc.) may be suitable in various applications Made with OpenOffice.org 2
Classes of number representation Special codes: BCD, Grey, etc. to meet certain technical requirement / or to avoid physical phenomena Fixed-point codes unsigned NBC, unsigned fractions signed signed-magnitude, biased, 2's-complement signed fractions Redundant number systems Residue Number System Floating-point numbers slash number system (for rational numbers) logarithmic number system exponential number system (IEEE754) Made with OpenOffice.org 3
Basics r radix [0, r-1] standard digit set for r-radix numbering system [0...max] natural number range with n-digits max+1 different number representations max = r n 1 How many positions (digits) are needed to represent natural numbers in arbitrary range [0... max]? n= floor[ log r max ] 1=ceil [log r max 1 ] Made with OpenOffice.org 4
Optimal radix 1. Compact number representation 2. Convenient physical realization 3. Simple arithmetic algorithms What is the best numbering system to represent number in range [0...max]? Optimization criterion for r-radix system: E(r) = r * n (n-digit positions) ln max 1 E r =r n=r log r max 1 =r ln r de dr =ln max 1 ln r 1 ln 2 r =0 r optimal =e=2.71 Optimal radix-3 or radix-2 system (more practical) E 2 E 10 =1.056, E 3 E 2 =1.5 =ln max 1 r ln r Made with OpenOffice.org 5
Unit in the least position (ulp) Fixed-point representation: n-whole digits + l-fractional digits For l=0, number is ordinary integer. n 1 x n 1 x n 2... x 1 x 0 x 1 x 2... x = l i= l x i r i ulp=r l For integer representation (l=0), ulp = 1 Maximal number with n+l fixed-point representation: max=r n 1 r l =r n 1 ulp Made with OpenOffice.org 6
Signed number representations How to represent negative numbers with digits only? (with view of computer application) [ N, +P ]?? [ 0, max ] x?? f, 0 f max -500... 499-2.6... +7.3-1.00... +0.99??? 000... 999 0.0... 9.9 0.00... 2.00 What are the benefits of different representation systems, with special interest in doing machine arithmetic operations? Made with OpenOffice.org 7
Signed-magnitude Converting signed numbers into unsigned form by indicating sign with the leftmost digit Easiest application in binary system: sign = most significant bit Advantages intuitive representation symmetric range simple negation 1110-6 1111-7 0000 +0 0001 +1 0010 +2 Increment 1101-5 1100-4 1011-3 1010-2 1001-1 1000-0 + 0111 +7 0110 +6 0011 +3 0100 +4 0101 +5 Increment Made with OpenOffice.org 8
Signed-magnitude Complex adder hardware: sign detector, magnitude comparator, subtractor circuit or internal complement conversions z = x + y or z = y x or z = x y = (y x) sign x sign y magnitude x magnitude y cmpl x cmpl watch out for two representation of 0 add/sub Control cout cin Adder sign cmpl z cmpl cout sign z magnitude z Made with OpenOffice.org 9
Biased representation Converting signed numbers into unsigned by addition of bias value to all numbers. Other name: excess-bias, e.g. excess-8, excess-128 [ N, +P] [ bias, max bias ] [ 0, max ] bias = N max = N + P = bias + P [ 8, +7 ] [0, 15] [ 0.50, +0.50] [0.00, 1.00] [ 31.5, 7.2] [0, 38.7] Add/Sub arithmetics needs additional operation (+/ bias) addition: x+bias + y + bias = x + y + 2*bias bias subtraction: x+bias (y + bias) = x y + bias negation: (x + bias) = 0+bias (x+bias) = bias x comparison: easy other oper.: difficult Made with OpenOffice.org 10
Biased representation For binary n-bit numbers and bias = 2 n-1, leftmost bit indicate sign 0 negative 1 positive 1110 +6 1111 +7 0000-8 0001-7 0010-6 Increment 1101 +5 1100 +4 1011 +3 1010 +2 1001 +1 + 1000 0 0111-1 0110-2 0011-5 0100-4 0101-3 Increment For binary n-bit numbers and bias = 2 n-1, the weight of leftmost bit is equal to bias value + bias : add 1 to the leftmost bit (complement) bias : sub 1 from the leftmost bit (complement) negation: (x + bias) = bias x = 00...0 x = 11...1+1 x = 11...1 x + 1 = x bit_compl + 1 Made with OpenOffice.org 11
Complement representation Converting signed numbers into unsigned by representing negative values x as the unsigned value M x [ N, +P ] [ 0, N + P ] no overlap of codes M > N + P max. code efficiency M = N + P + ulp M-2 M-1 0 +1 (ulp) [ 4, +11 ] [ 0, 15 ], M = 16 3 = 13 [ 4.00, +3.99] [0.00, 7.99], M = 8.00 2.77 = 5.23 Arithmetic modulo-m: x + y = x + y if x + y P x y = x + (M y) = = x y if x y = M (y x) if y > x y x = y + (M x) = = y x if y x = M (x y) if x > y N M N + +P x y = (M x) + (M y) = = M (x + y) if x+y > N Made with OpenOffice.org 12
Complement representation Arithmetic requirements: negation(m x) : subtraction modulo-m : division/subtraction Selection of M must simplify those operations For r-radix and n-digit fixed-point (symmetrical range) representation: M = r n - radix complement modulo-m: ignoring the carry-out from leftmost position in add-operations negation: each digit-complementation (r 1 digit) and plus ulp to the total M = r n ulp - digit (diminished-radix) complement modulo-m: negation: end-around carry each digit-complementation (r 1 digit) Made with OpenOffice.org 13
2's complement For radix=2 and n-whole digit representation: M=2 n asymmetric range 2 n-1... 2 n-1 ulp x-complement: modulo-m: 2 n x = [(2 n ulp) x] + ulp = 11...1 x + ulp = x bit_ompl + ulp (overflow possible) ignoring the carry-out from leftmost position Increment 1101-3 1100-4 1011-5 1110-2 1010-6 1111-1 1001-7 0000 0 1000-8 0001 1 + 0111 7 0010 2 0110 6 0011 3 0100 4 0101 5 Increment Made with OpenOffice.org 14
2's compl. with fractional digits Binary complement representation (M=2): 1-whole digit & l-fractional digits, e.g. 1.011 range [ 1, +1 ulp] [-1.000, 0.875], ulp=0.125, l=3) 1-whole bit sign Increment 1.101-0.375 1.100-0.500 1.011-0.625 1.111-0.125 1.110-0.250 1.010-0.750 1.001-0.825 0.000 0 1.000-1.000 0.001 0.125 + 0.010 0.250 0.110 0.750 0.111 0.875 0.011 0.375 0.100 0.500 0.101 0.625 Increment Made with OpenOffice.org 15
1's complement For radix=2 and n-digit representation: M=2 n ulp symmetric range 2 n-1 +ulp... 2 n-1 ulp x-complement: (2 n ulp) x = 11...1 x = x bit_ompl (no overflow) modulo-m: ignoring the carry-out from leftmost position and inserting carry to the rightmost position (carry out carry in: end-around carry) double representation of 0: 00...0 and 11...1 no problems in arithmetic modulo-m operations 0 can be left intact (or automatically converted to 0 by hardware) 0 modulo M 0 Made with OpenOffice.org 16
1's complement 1110-1 1111-0 0000 0 0001 1 0010 2 Increment 1101-2 1100-3 1011-4 1010-5 1001-6 1000-7 + 0111 7 0110 6 0011 3 0100 4 0101 5 Increment Made with OpenOffice.org 17
Radix vs digit complement Feature Radix complement Digit complement symmetry no for even radix, yes for even radix, yes for odd radix no for odd radix unique zero yes no complementation x bit_compl + ulp x bit_compl modulo-m addition drop carry-out end-around carry Odd-radix systems have no practical applications Made with OpenOffice.org 18
Efficient complementation in 2's Calculation of -x (x-complement) is slow in 2's complement big disadvantage! However, during subtractions it can be eliminated by simultaneous adding of cin=1, so there is no need for additional arithmetic operation. x y = x + y bit_ompl + ulp x y bit_cmpl add(0)/sub(1) Adder cin cout z Made with OpenOffice.org 19
Extension of 2's & 1's complement n-whole digits & l-fractional digits representation n' & l' representation (n'>n, l'>l) Complementation of positive number is always padding it with 0's, from left and right. Complementation constant M changes from 2 n to 2 n' during conversion (2's complement), thus the difference 2 n' 2 n must be added to old (n&l) representation. 2 n' 2 n contains ones at positions n'-1...n. 2-s complement extension is done by: left: replicating the sign bit (sign extension) right: padding with 0's Complementation constant M changes from 2 n 2 -l to 2 n' 2 -l' during conversion (1's complement), thus the difference 2 n' 2 n + 2 -l' 2 -l must be added to old (n&l) representation. 2 n' 2 n + 2 -l 2 -l' contains ones (1) at positions n'-1...n and positions -(l+1)...-l' 1-s complement extension is done by: left: replicating the sign bit (sign extension) right: replicating the sign bit (sign extension) Made with OpenOffice.org 20
Direct vs indirect sign arithmetic Application of direct or indirect computation approach depends on available algorithms. For some cases, direct algorithms are inefficient, so indirect approach is more universal. Process of sign removal is trivial for all popular signed representations. Sign removal is just one example of preprocessing of arguments. Other examples are: restricting the value to prescribed range (e.g. in trigonometry) detecting restricted values (e.g. in division) scaling the arguments x y x y Signed operation Sign logic Sign removal Unsigned operation Sign correct. f(x,y) f(x,y) Made with OpenOffice.org 21