Download presentation
1
Fixed-Point Arithmetics: Part I
2
Binary Representation
Unsigned Magnitude Signed Magnitude Two’s Complement
3
Unsigned Magnitude Only positive number are presented No sign bit
For N bits we can represent the signed integers between 0 and 2N -1 Example: 111 -> 7
4
Signed Magnitude The most significant bit is used to represent the sign: “1” means negative, “0” means positive For positive numbers the result is the same as unsigned magnitude representation For N bits we can represent the signed integers between -2(N-1) +1 and +2(N-1)-1
5
Disadvantage of Signed Magnitude
Two encoding for the zero ‘0’. Addition of two numbers of opposite sign will not yield 0!! = (85)H + (05) H = (8A) H = -10!! Arithmetic operations of numbers with unlike sign and with same sign must be handled differently (subtracter needed with unlike signs) A – B ≠ A + (-B) Example: 3 – 2 Addition Operation 3 + (-2) Subtraction Operation (3-2) 0011 (+3) (+3) (-2) (+2) 1100 (-5) WRONG! (+1) CORRECT!
6
Disadvantage of Signed Magnitude
A hardware adder and subtracter needed Special treatment also for A – B if B > A Example 3 – 2 & 2 – 3 Subtraction Operation Subtraction Operation 2 - 3 0011 (3) (+2) (2) (+3) 0001 (1) CORRECT (-7) WRONG Solution: Switch order of operands (3 – 2 instead of 2 – 3) Perform subtraction (Result = 1) attach the minus sign (Result = -1)
7
Two’s Complement example
1 = −1 = = 1111 2 = −2 = = 1110 6 = −6 = = 1010 8 = 1000 −8 = = 1000 Note that the max positive number that can be represented is 7! Since to get the negative of -8 we need: -8 = 1000 To bitwise invert : 0111 Add +1 to LSB: +1 Result: So –(-8) = -8 (abnormal result) 8 cannot be represented For N bits, the max number is 2(N-1)-1 while the min number is -2(N-1)
8
Geometric Depiction of Twos Complement Integers
9
Benefits of 2’s Complement
One unique representation of the number 0: 0 = Complement: Add +1 to LSB: +1 Result: (Ignoring carry bit) -0 = +0 Negation: take complement and add +1 Extending word length: For positive number pack with leading zeros +3 =011 +3 = For negative numbers pack with leading ones -4=100 -4 =111000
10
Benefits of 2’s Complement
Subtraction Rule: to subtract ‘b’ from ‘a’, take the 2’s complement of ‘b’ and add it to ‘a’: a – b = a + (-b) a b b a Signed Mag 2’s Compare signs bits Equal?? No Adder Yes If Subtraction enable Adder: a + b Yes Is a > b No Result 2’s Comp Sub: a - b Swap Operand Sub: a – b Negate
11
Benefits of 2’s Complement
Overflow Rule: if two numbers with same sign are added and overflow occurs the result number has different sign: Addition: = 110 = -2 Subtraction: 110 – 011 = 011 = 3 Multiplication: 011 * 010 = 110 3 * = -2
12
What to Do In case of Overflow
Prevention rule: design your code with minimal possible overflows DSP can be configured to saturate the number at overflow (N=4): 5 x 2 = 0010 Overflow 5 x 2 = 0111 Saturate Guard bits in accumulator could be useful for intermediate results Example: 5 x x 4 = 10 – 4 = 6 If accumulator has one extra bit called “guard bit” the final result will not overflow!
13
Multiplication Example
Multiplicand (11 dec) x Multiplier (13 dec) Partial products Note: if multiplier bit is 1 copy multiplicand (place value) otherwise zero Product (143 dec) Note: need double length result
14
Unsigned Binary Multiplication
15
Execution of Example
16
Flowchart for Unsigned Binary Multiplication
17
Problem: Multiplication & 2’s Complement
Addition and Subtraction can be treated as unsigned integer 1001 (+9) + 0011 (+3) 1100 (+12) 1001 (-7) + 0011 (+3) 1100 (-4) 2’s Complement Unsigned What about multiplication?? 1010 (10) x 1110 (14) 0000 (140) 1010 (-6) x 1110 (-2) 0000 (-116) 2’s Complement Unsigned Wrong Result!!!
18
Problem: Multiplication & 2’s Complement
Addition and Subtraction can be treated as unsigned integer 1001 (+9) + 0011 (+3) 1100 (+12) 1001 (-7) + 0011 (+3) 1100 (-4) 2’s Complement Unsigned What about multiplication?? 1010 (10) x 1110 (14) 0000 (140) 1010 (-6) x 1110 (-2) 0000 (-116) 2’s Complement Unsigned Need to sign extend partial products Wrong Result!!!
19
Problem: Multiplication & 2’s Complement
Addition and Subtraction can be treated as unsigned integer 1001 (+9) + 0011 (+3) 1100 (+12) 1001 (-7) + 0011 (+3) 1100 (-4) 2’s Complement Unsigned What about multiplication?? 1010 (10) x 1110 (14) 0000 (140) 1010 (-6) x 1110 (-2) 111010 11010 (44) 2’s Complement Unsigned Need to sign extend partial products Still Wrong Result!!!
20
Multiplying Negative Numbers
This does not work! Solution 1 Convert to positive if required Multiply as above If signs were different, negate answer Solution 2 Booth’s algorithm
21
Solution 1 Solution 1 is as follows:
If either multiplicand or multiplier is negative convert it to positive Do the multiplication operation Check the signs of the multiplicand and multiplier if different negate the result Adds Hardware Complexity
22
Solution 2: Booth Algorithm (1951)
Booth starts from the fact that a series can be represented as: Suppose that we have to perform the following multiplication: M*30 -> M * ( ) = M * ( ) From series the multiplication can be written as: M * ( ) = M * (25 – 21) Savings on computations
23
Booth Algorithm: Multiply the multiplicand by -2n-K when a transition from 0 to 1 (0-1) is encountered going from right to left. Multiply the multiplicand by 2n+1 whenever an transition from 1-0 is encountered Add the partial products obtained.
24
Booth Algorithm: When multiplying 2n-bit numbers, form 22n-bit partial products as follows: When the first 1 of a block is encountered (1-0), partial product is formed by negating the multiplicand and appropriately shifting the negated multiplicand When the last 1 of a block is encountered (0-1), partial product is formed by taking the multiplicand and appropriately shifting it When either 0-0 or 1-1 is encountered, partial product is all zeros Sign-extend all partial products Add the partial products to obtain the multiplication result.
25
Booth Algorithm: Example 1
Multiplying: -6 x -2 1010 -6 X 1110 (0) -2 0-0 1-0 (Subtract multiplicand) 000000 1-1 (Shift) 00000 1-1 (Shift)
26
Multiplication: Why MSB is Redundant?
Assume that we have a number represented by 4 bits (N = 4) 2’s complement range is from -8 to +7. The min/max number obtained from multiplication is -56/+64 7 bits are enough to represent the result. In general: 2N-1 bits are needed if NxN multiplication is performed The additional MSB is a “sign extension bit” and can be removed Another way to interpret it is that if converted to unsigned the multiplication result will be (N-1) + (N-1) + 1 sign bits giving 2N – 1.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.