Download presentation
Presentation is loading. Please wait.
Published byAnabel Gardner Modified over 9 years ago
1
Computer Arithmetic — Number Representation Paolo.Ienne@epfl.ch EPFL – I&C – LAP Vojin.Oklobdzija@epfl.ch EPFL – CSDA and UC Davis – ACSEL
2
© Ienne, Oklobdzjia 2004CompArith — Number Representation2 Why Representation? Need to map numbers to binary bits Need a representation anyway… Representation is consequential Complexity of arithmetic operations depends heavily on the representation Number representation is the heart of computer arithmetic…
3
© Ienne, Oklobdzjia 2004CompArith — Number Representation3 What Is a Number System? A number is represented as an ordered n-tuple of symbols (digit vector) Each symbol is a digit Digits usually represent integers from a given set—e.g.,
4
© Ienne, Oklobdzjia 2004CompArith — Number Representation4 Rule of Interpretation Mapping from set of digit vectors to numbers (e.g., integers, reals) “twelve” digit vectorsN, Z, R,…
5
© Ienne, Oklobdzjia 2004CompArith — Number Representation5 Redundancy digit vectorsN, Z, R,… Nonredundant digit vectorsN, Z, R,… Redundant
6
© Ienne, Oklobdzjia 2004CompArith — Number Representation6 Positional Weighted Systems The rule of interpretation is a scalar product where is the weight vector
7
© Ienne, Oklobdzjia 2004CompArith — Number Representation7 Radix Systems Weights are not arbitrary but related to a radix vector in the following way
8
© Ienne, Oklobdzjia 2004CompArith — Number Representation8 Mixed-Radix Systems Fixed-radix if all elements of R are identical A few mixed-radix systems are very common—e.g., time
9
© Ienne, Oklobdzjia 2004CompArith — Number Representation9 Common Decimal Notation It is weighted It is positional It is fixed-radix
10
© Ienne, Oklobdzjia 2004CompArith — Number Representation10 Common Decimal Notation It is nonredundant because canonical 10 n possible digit vectors to represent 10 n values Weighted, positional, fixed-radix, nonredundant also called conventional systems
11
© Ienne, Oklobdzjia 2004CompArith — Number Representation11 Digit Set Canonical set A canonical system is nonredundant Any other choice is noncanonical
12
© Ienne, Oklobdzjia 2004CompArith — Number Representation12 Why Redundant Representations? Remember paper & pencil addition? In binary: Slow—critical path contains n full-adders: naïve delay is O(n) Must be performed right to left (LSB to MSB) FA a n-2 b n-2 s n-2 FA a n-1 b n-1 s n-1 C out C in FA a1a1 b1b1 s1s1 a0a0 b0b0 s0s0
13
© Ienne, Oklobdzjia 2004CompArith — Number Representation13 Redundancy May Simplify Some Operations Radix-2, noncanonical {0, 1, 2, 3}, 2 bits per digit digitencoding aiai ai”ai”ai’ai’ 000 101 210 311 + AB S 2n FA a i-1 ” b i-1 ” ai’ai’ bi’bi’ si”si”si’si’ Critical path now contains only two full-adders: delay is now O(1)
14
© Ienne, Oklobdzjia 2004CompArith — Number Representation14 Redundancy and MSB-first? Now that the carry chain is broken, output digit i depends exclusively on input digits i, i – 1, i – 2 Hence I can receive the operands in serial form and, when I have seen the first three input digits, I can produce the first output digit; afterwards, every new input digit, I can produce a new output digit Online arithmetic or MSDF digit-serial arithmetic FA a i-1 ” b i-1 ” ai’ai’ bi’bi’ si”si”si’si’ FA a i-2 ”a i-1 ’ b i-1 ’
15
© Ienne, Oklobdzjia 2004CompArith — Number Representation15 Very Large Choice of Weighted Positional Number Systems Source: Parhami, © Oxford 2000
16
© Ienne, Oklobdzjia 2004CompArith — Number Representation16 Binary Representation and Negative Numbers Simplest signed system: Sign-and-magnitude One bit represents the sign (typ. 1 neg.) Absolute value represented as unsigned Exactly as humans do in decimal Of course, we could also write 0 for + and 9 for – …
17
© Ienne, Oklobdzjia 2004CompArith — Number Representation17 Sign-and-Magnitude Representation Some advantages and disadvantages Familiar for users Simple naïve multiplication Adders are not the most efficient Redundant zero (+0 and –0) may cause some problems (e.g. testing for a variable =0)
18
© Ienne, Oklobdzjia 2004CompArith — Number Representation18 Negative Numbers As Result of Paper & Pencil Subtraction Consider a normal paper-and-pencil subtraction 11 11 11 11 00001010202 10 00010001212 17 10 ……1111001212 11 1111001212 2727 2626 2525 2424 2323 2020 7 10
19
© Ienne, Oklobdzjia 2004CompArith — Number Representation19 Intuitive Derivation of the Two’s Complement System Two’s complement representation Largest (positive): all ones except a n-1 = 0 Smallest (negative): all zeros except a n-1 = 1 Asymmetric range, non-redundant Standard in computers for signed integers nowadays
20
© Ienne, Oklobdzjia 2004CompArith — Number Representation20 Comparison of Binary Representations on 3 Bits Decimal Binary (Unsigned) Sign and Magnitude Two’s Complement 7111 6110 5101 4100 3011 2010 1001 0000000 / 100000 101111 -2110 -3111101 -4100
21
© Ienne, Oklobdzjia 2004CompArith — Number Representation21 Two’s Complement Representation of Negative Numbers It “shifts” the second half of the unsigned range into the negative by 2 n E.g., on 8 bits: 000000000111111111111111 0127255 10000000 128 0111111110000000 127-128 11111111 00000000 0 Z (signed) N (unsigned)
22
© Ienne, Oklobdzjia 2004CompArith — Number Representation22 True-and-Complement Systems for Signed Numbers Transform signed numbers in unsigned and then use conventional systems Z (signed)N (unsigned)digit vectors “minus two”“six”
23
© Ienne, Oklobdzjia 2004CompArith — Number Representation23 Mapping from Signed to Unsigned A number A is represented through the unsigned number A R defined through a complementation constant This is equivalent (for |A| < C ) to For the representation to be unambiguous, it must be or at most equal… True forms Complement forms
24
© Ienne, Oklobdzjia 2004CompArith — Number Representation24 Inverse Mapping Inversely If A R = C / 2 can be represented, it can be assigned either to A = – C / 2 or to A = C / 2, but the representation is then asymmetric ( system not close under sign change operation) If A R = C is can be represented, there are two representations for zero
25
© Ienne, Oklobdzjia 2004CompArith — Number Representation25 Range Complement and Digit Complement Systems Two particular cases of true-and-complement systems Range complement: C = r n Digit complement: C = r n – 1 In radix-2 Two’s complement: C = 2 n One’s complement: C = 2 n – 1 Two’s complement is asymmetric and nonredundant One’s complement is symmetric with a redundant zero
26
© Ienne, Oklobdzjia 2004CompArith — Number Representation26 Comparison of More Binary Representations on 3 Bits Decimal Binary (Unsigned) Sign and Magnitude Two’s Complement One’s Complement 7111 6110 5101 4100 3011 2010 1001 0000000 / 100000000 / 111 101111110 -2110 101 -3111101100 -4100
27
© Ienne, Oklobdzjia 2004CompArith — Number Representation27 Equivalence of the Two Two’s Complement Definitions Using the true-and-complement definition, if A R < C / 2 If A R ≥ C / 2 Hence the property
28
© Ienne, Oklobdzjia 2004CompArith — Number Representation28 Similar Property for One’s Complement Using the true-and-complement definition, if A R < C / 2 If A R ≥ C / 2 Hence the property
29
© Ienne, Oklobdzjia 2004CompArith — Number Representation29 Sign For sign-and-magnitude is clearly trivial… For true-and-complement too, it is For all these representations Sign bit
30
© Ienne, Oklobdzjia 2004CompArith — Number Representation30 Implied Digits in the Unsigned Representation Unsigned numbers (like naturals in decimal) have infinite leading zeros in front of them: 1 234 10 …00 001 234 10 1 0101 2 …0 0000 0001 0101 2 Changing the representation from n bits to m bits ( m > n ) is just a matter of padding the number with leading zeros
31
© Ienne, Oklobdzjia 2004CompArith — Number Representation31 Changing the Number of Bits in One’s and Two’s Complement Signed numbers can be thought as having infinite replicas of the MSB/sign in front of them “Minus three” on 4 bits is not represented as “minus three” on 8 bits! 1101212 4 bits 2323 2222 2020 3 10 11111101212 8 bits 2727 2626 2525 2424 2323 2222 2020 3 10
32
© Ienne, Oklobdzjia 2004CompArith — Number Representation32 Sign Extension and Shift Right Two’s complement representation on n bits Sign extended two’s complement representation on m bits ( m > n ) To extend from n to m bits, one adds (for positive #) zero or (for negative #)
33
© Ienne, Oklobdzjia 2004CompArith — Number Representation33 Addition in True-and-Complement If there is no overflow (that is, we assume the result of the sum to be representable), S = A + B can be simply computed as Easy to prove using (X mod Y) mod Y = X mod Y
34
© Ienne, Oklobdzjia 2004CompArith — Number Representation34 Two’s Complement Addition Particularly comfortable in two’s complement, because C = 2 n and performing mod 2 n implies just to ignore the carry out bit Remember the intuitive derivation of the two’s complement representation… + AB S C out C in 0
35
© Ienne, Oklobdzjia 2004CompArith — Number Representation35 One’s Complement Addition A little more complex in one’s complement, because C = r n – 1 and the modulo of W R = A R + B R is less easy Hence
36
© Ienne, Oklobdzjia 2004CompArith — Number Representation36 One’s Complement Addition End-around carry + AB S C out C in
37
© Ienne, Oklobdzjia 2004CompArith — Number Representation37 Negation Bitwise negation where, as usual in binary, Example in radix-2 on 4 bits:
38
© Ienne, Oklobdzjia 2004CompArith — Number Representation38 Complement and Negation in True-and-Complement Fundamental property in n -digit r -radix systems because a n – 1 a n – 2 …a2a2 a1a1 a0a0 +A a n – 1 a n – 2 …a2a2 a1a1 a0a0 =A r – 1 … + 1=1 100…000r n
39
© Ienne, Oklobdzjia 2004CompArith — Number Representation39 Change of Sign In one’s complement In two’s complement
40
© Ienne, Oklobdzjia 2004CompArith — Number Representation40 Change of Sign in Two’s Complement Another way of verifying the property Use two’s complement property/definition
41
© Ienne, Oklobdzjia 2004CompArith — Number Representation41 Two’s Complement Subtracter + AB S C out C in 1
42
© Ienne, Oklobdzjia 2004CompArith — Number Representation42 Two’s Complement Adder/Subtracter Overflow?! + AB S C out C in Sub
43
© Ienne, Oklobdzjia 2004CompArith — Number Representation43 Overflows for Unsigned Addition Limited range, thus sum of two numbers can exceed the range Overflows if carry out is 1—that is, if the first bit which cannot be stored is not null 11001001212 201 10 11001000202 200 10 110010001212 401 10 (>255 10 )
44
© Ienne, Oklobdzjia 2004CompArith — Number Representation44 Overflows for Signed Addition The situation is somehow different and the same simple rule does not hold anymore Carry out is 1, but now it is ok because the result is negative (think of sign extension…) 11001001212 -55 10 11001000202 -56 10 110010001212 -111 10 10010001212
45
© Ienne, Oklobdzjia 2004CompArith — Number Representation45 Overflows for Signed Addition Overflows makes sum of positives look like negative or sum of negatives look like positive 11111111 127255 10000000 128 10000000 -128 11111111 0 1111111100000000 90 10 = 0101 1010 2 105 10 = 0110 1001 2 195 10 = 1100 0011 2 -61 10 = 1100 0011 2 + =
46
© Ienne, Oklobdzjia 2004CompArith — Number Representation46 Overflows Addition and Subtraction No overflow possible if adding operands of different sign Overflow only if Hence OpABResult A B 0 0 A B 0 0 A B 0 0 A B 0 0
47
© Ienne, Oklobdzjia 2004CompArith — Number Representation47 Overflows Addition Considering all possible cases around the last FA a n ̶ 1 b n ̶ 1 c n ̶ 1 cncn s n ̶ 1 Overflow 00000No 00101Yes 01001No 01110 10001 10110 11010Yes 11111No
48
© Ienne, Oklobdzjia 2004CompArith — Number Representation48 Another View on Overflow for Signed Addition Sign extend the operands and check if the additional result bit is equal to the pervious one (sign “unextension”…) 111001001212 -55 10 111001000202 -56 10 110010001212 -111 10 10010001212
49
© Ienne, Oklobdzjia 2004CompArith — Number Representation49 Fixed Point In real life, one does not only need integers… If one adds a fractional point in a fixed position, hardware for integers works just as well It’s just a matter of representation! 01001212 9 10 00011212 3 10 01100202 12 10 2424 23232 2121 2020 01.001212 1.125 10 00.011212 0.375 10 01.100202 1.500 10 2121 2020 2 -1 2 -2 2 -3
50
© Ienne, Oklobdzjia 2004CompArith — Number Representation50 Fixed Point Requires Attention from Programmers Multiplication often introduces the need of arithmetic shifts to the right Note the low accuracy! 0.1010202 0.625 10 0.0110202 0.375 10 0.00111100202 0.234375 10 0.0011212 0.1875 10
51
© Ienne, Oklobdzjia 2004CompArith — Number Representation51 References M. D. Ercegovac and T. Lang, Digital Arithmetic, Morgan Kaufmann, 2004 I. Koren, Computer Arithmetic Algorithms, Peters, 2002 A. R. Omondi, Computer Arithmetic Systems— Algorithms, Architecture and Implementation, Prentice Hall, 1991 B. Parhami, Computer Arithmetic—Algorithms and Hardware Designs, Oxford, 2000
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.