Chapter 2 Positional number systems A positional number system represents numeric values as sequences of one or more digits. Each digit in the representation is weighted according to its position in the number. The weights increase towards the left by a constant factor called the base or radix (the Latin word for root ). The positions corresponding to integral weights ( ) are separated from the positions corresponding to fractional weights (< ) by the radix point (. ). 0 2 + 0 + 0 0. 0 + 0 2 365.24 0 = 3 00 + 6 0 + 5. 2 0. + 4 0.0 Generalised for any radix R: R 2 R R 0. R R 2 stu.vw r = s + t + u + v + w Some commonly used bases are: R = 2 8 0 6 name binary octal decimal hexadecimal A radix R representation uses R distinct digits, starting with 0. Binary uses two digits, 0 and. Octal uses eight digits, 0 through 7. Hexadecimal requires 6 digits and uses the first ten decimal digits ( 0 to 9 ) followed by the first six letters ( A to F, representing digit values 0 through 5). Arithmetic performed on a single digit produces results modulo R (ignoring carry). Arithmetic on a -digit number produces results modulo R. The representations of nr + k and k are the same, and nr 0. 2. Signed number representations binary octal decimal hexadecimal 0000 0 0 0 000 000 2 2 2 00 3 3 3 000 4 4 4 00 5 5 5 00 6 6 6 0 7 7 7 000 0 8 8 00 9 9 00 2 0 A 0 3 B 00 4 2 C 0 5 3 D 0 6 4 E 7 5 F The sign of a number indicates whether or not it is negative. The magnitude (or absolute value ) of a number x, written x, is the distance of that number from 0. x, x 0 x = x, x < 0 A signed number representation encodes both the sign and the magnitude of a value as an unsigned number, using a fixed number of radix-r digits. 2.. Signed magnitude 4
A negative value x is represented as R/2 + x. The most significant digit encodes the sign; if it is R/2 or greater then the number is negative. Subtracting R/2 from a negative number recovers its magnitude. With signed magnitude representations, non-negative values are represented as their unsigned value. There are two representations for zero (+0 and 0), and +0+ 0= 0. Additive arithmetic operators and comparisons are more complex than with other representations. Division is simpler than with other representations [Sec. 3.4], and signed magnitude is used to represent the mantissa of floating-point numbers. 7-bit magnitude signed magnitude decimal binary value R =0, =2 R =2, =8 49 49 00000 20 20 000000 5 5 0000 0 0000000 0 50 0000000 5 000000 5 65 000 20 70 00000 49 99 0000 7-bit magnitude 0 0 0 0 0 x 4 = 4 0 0 0 0 - x 4 = -4 positive sign negative sign 2..2 Biased Any number x is represented as x + k (where k is a constant), allowing values in the range [ k, R k] to be represented. Subtracting k from the stored number recovers its signed value. Typically k = R /2 so that zero falls in the middle of the range of representable values. o non-negative value is represented as its unsigned value. So, while there is only one representation for 0, it is not the same as unsigned 0. Arithmetic operators are difficult to implement, and 0+0 6= 0(because k+k 6= k, mod R, except when k =0which is not useful). Biased representations are used to store the exponent of floating-point numbers, where only relative magnitude of exponents is important. 2..3 Diminished radix complement A negative number x is represented by R x. As with signed magnitude representation, the most significant digit encodes the sign of the number, which is negative if the S digit is R/2 or greater. R x can be formed by individually inverting each digit x i in x. The inverse of a digit d is R d [Fig. 2.]. (Inverting a binary digit just exchanges 0 and, and is often called flipping the bit.) This works because = = R i (R x i ) (individually inverted digits x i ) R i R i= X R i R i R i = R R 0 R i x i R i x i X R i x i = R x. ( the diminished radix complement of x) 5
digit 0 2 3 4 5 6 7 8 9 A B C D E F inverted binary digit 0 inverted octal digit 7 6 5 4 3 2 0 inverted decimal digit 9 8 7 6 5 4 3 2 0 inverted hexadecimal digit F E D C B A 9 8 7 6 5 4 3 2 0 Figure 2.: Inverse digits ( negative sign digits highlighted) If R is fixed then the dimished radix complement can be called the (R ) s complement. For decimal it is the nine s complement, and for binary the one s complement. Using a fixed number of digits (arithmetic modulo R ), a number x and its diminished radix complement x sum to zero. Unfortunately there are two representations for zero (+0 6= 0) and x + x = 0(modR ). Worse, 0+ 0= (modr ). Diminished radix complement is seldom used as a representation for storing signed numbers. Computing the diminished radix complement in hardware is, however, very efficient. It is used in the ALU to implement binary arithmetic [Sec. 3.2] and logic [Sec. 5..] operations. 9 s and s complement value decimal binary 49 49 00000 20 20 000000 5 5 0000 0 0000000 0 99 98 0 5 84 0000 20 79 00 49 50 000 2..4 Radix complement A negative number x is represented by R x. This can be formed by taking the diminshed radix complement of x and adding to it. The most significant digit still encodes the sign of the number, which is negative if the S digit is R/2 or greater. Using a fixed number of digits (arithmetic modulo R ), a number x and its radix complement x sum to zero. There is only one representation of zero, and it has non-negative sign. Addition and subtraction of any two signed values using radix complement representation yields the correct result (modulo R ) provided the result does not exceed the range of values that can be represented in digits [Secs. 3., 3.2]. Adding when negating increases the range of representable negative values by one, making it asymmetric value decimal binary 0 s and 2 s complement ([ R,R ]). The most negative value ( R ) therefore cannot be negated, since the result is one larger than the 28 27 0 50 00000 maximum representable positive value. 49 49 00000 For a radix R, the dimished radix complement is often 20 20 000000 called the R s complement. For decimal it is the ten s complement, and for binary the two s complement. 0 0000000 5 5 0000 The final addition of could cause every digit to change, as carries are propagated from least to most significant 99 digit [Sec. 3.], but this addition can often be peformed implicitly with no additional overhead [Sec. 3.2]. 20 80 000 5 85 000 Two s complement is the most common representation 49 5 00 50 50 000 for both storing and performing arithmetic on signed binary 27 000000 numbers in digital computers. 28 0000000 6
2.2 Radix conversion Converting between binary and either octal or hexadecimal is trivial. To convert to/from octal (base 8=2 3 ), consider groups of three bits. To convert to/from hexadecimal (base 6 = 2 4 ), consider groups of four bits. (Between octal and hexadecimal, go through binary.) octal 7 5 3 binary 0 0 0 0 hexadecimal F 5 9 To convert a number to a representation in an arbitrary base, consider the integral and fractional parts separately, and extract one digit at a time. Let x R be a number in radix R representation. ultiplying x by R moves the radix point one position to the right (shifts the number one position to the left). Dividing x by R moves the radix point one position to the left (shifts the number one position to the right). Therefore: To extract the rightmost digit d from an integer x R, divide x by R and the remainder is d. To extract the leftmost digit d from a fraction x R, multiply x by R and the integral part of the result is d. Repeated application of these operations will convert a number into its representation in any base [Algs. 2., 2.2, 2.3]. Algorithm 2. Converting an integer into an arbitrary radix representation input: an integer n and a radix R procedure ITEGERTORADIX(n, R) q n div R. x div y is the integer quotient of x y d n mod R. x mod y is the remainder of x y if q 6= 0then ITEGERTORADIX(q, R) end if output digit d. output all digits to the left of d Algorithm 2.2 Converting a fraction into an arbitrary radix representation input: a fraction n and a radix R procedure FRACTIOTORADIX(n, R) repeat n n R d bnc. bxc is the integer part of x output digit D n n d. the fractional part of n until n =0 7
Algorithm 2.3 Converting a number into an arbitrary radix representation input: a number n and a radix R procedure UBERTORADIX(n, R) ITEGERTORADIX(bnc,R). output the integer part of n output radix point. FRACTIOTORADIX(n bnc,r). output the fractional part of n 2.3 Binary numbers Binary numbers have radix 2. When signed, they are usually stored using two s complement representaion. Binary arithmetic [Ch. 3] is usually peformed using two s complement representation, with one s complement being useful in a few situations [Sec. 3.4]. 2.3. Two s complement decimal conversions To convert a two s complement number to decimal, sum the weighted contributions of each bit (as if it were unsigned) but use (2 ) for the most significant bit (instead of 2 ). (2 7 ) 2 6 2 5 2 4 2 3 2 2 2 2 0 28 64 32 6 8 4 2 0 0 0 0 64 + 8 + 4 + 2 = 78 0 0 0 28 + 64 + 8 + 4 + 2 = 50 To convert a negative decimal to two s complement, add 2 to it, convert the resulting non-negative number to binary, and then set the sign bit to. The above follow from converting just the sign bit to a non-negative magnitude (by taking its two s complement) and then negating the result to obtain its positional weight. x = (2 2 )= 2 2.3.2 Width conversions A -bit unsigned binary number x is widened to >bits by setting bits x [,] to 0; this is called zero-extending x. arrowing from bits to bits is achieved by discarding bits x [,], with possible loss of information; this is called truncating x. A -bit signed binary number x is widened to >bits by copying the sign bit x into bits x [,] ; this is called sign-extending x. arrowing from bits to bits is achieved by discarding bits x[ :], with possible loss of information including arbitrary change of sign. unsigned zero-extend 0 0 0 000 widening signed =3 sign-extend 0 =3 0 unsigned 0 0 00 truncate 0 narrowing signed =29 0 truncate =3 0 00 0 0 0000 00 0 0 0000 =+5 =+5 0 0 0000 00 0 0 0000 00 =+9 =-7 8