EE457 Representing signed numbers in Two s Complement notation A way to view the signed number representation in 2 s complement notation as a positional weighted coefficient system. example ( 2) = (6 4 ) = 00000 = = 0x2 7 0x2 6 0x2 5 x2 4 0x2 3 x2 2 0x2 x2 0 How to represent -2 walking 2 steps backward is same as walking 256-2 = 235 steps forward on a 256-segment circle. 235 = 28 64 32 8 2 = 00. Hence (-2) is represented 00. - 4 2 3-3 - 5 Error point: C bit is set 0-2 - 6 4 0 00 0 0 00 0 9 5 0 0 8 0 7 0000 000 000 00 000 00 00 00 00 000 0-0 - 7 UNSIGNED - 8 7 0000 000 000 SIGNED 00 000 00 00 00 00 000 0 6 6 2 2 5 3 3 5 4 4 Error Point: V bit is set / 3 C Copyright 2006 Gandhi Puvvada
We prove that the weight of the most significant bit is a -2 7 where as the rest of the weights are all powers of 2 (2 6, 2 5, 2 4, 2 3, 2 2, 2, 2 0 ). i.e. 00 = = x(-2 7 ) x(2 6 ) x(2 5 ) 0x(2 4 ) x(2 3 ) 0x(2 2 ) x(2 ) x(2 0 ) = - 28 64 32 0 8 0 2 = - 28 07 = - 2 Proof: Take some random negative number B = b 6 b 5 b 4 b 3 b 2 b b 0. To find the value of 2 s complement of B, we find (256 - B) = (255 - B) = ( - b 6 b 5 b 4 b 3 b 2 b b 0 ) = [ ( - )x2 7 ( - b 6 )x2 6 ( - b 5 )x2 5 ( - b 4 )x2 4 ( - b 3 )x2 3 ( - b 2 )x2 2 ( - b )x2 ( - b 0 )x2 0 ] Ignore ( - )x2 7 and process other terms. Gathering all s together in the above right-hand side expression, we get = [ {(x2 6 x2 5 x2 4 x2 3 x2 2 x2 x2 0 ) } - { b 6 x2 6 b 5 x2 5 b 4 x2 4 b 3 x2 3 b 2 x2 2 b x2 b 0 x2 0 } ] = [{x2 7 } - { b 6 x2 6 b 5 x2 5 b 4 x2 4 b 3 x2 3 b 2 x2 2 b x2 b 0 x2 0 }] 2 / 3 C Copyright 2006 Gandhi Puvvada
Hence the value of B = - [{x2 7 } - { b 6 x2 6 b 5 x2 5 b 4 x2 4 b 3 x2 3 b 2 x2 2 b x2 b 0 x2 0 }] = x(-2 7 ) b 6 x(2 6 ) b 5 x(2 5 ) b 4 x(2 4 ) b 3 x(2 3 ) b 2 x(2 2 ) b x(2 ) b 0 x(2 0 ) Example: Find the value of the negative number 00 (a 6-bit number represented in 2 s complement notation. -2 5 2 4 2 3 2 2 2 2 0-32 6 8 4 2 0 0 = (-32) (8 4 2) = (-32) (4) = -8 Verify. Negate the number and find the value of the corresponding number. Flip bits: 0 0 0 0. Add : 0 0 0 0. Find value: 6 2 = 8. Hence the give number is - 8. ======================================================================== 3 / 3 C Copyright 2006 Gandhi Puvvada
Addition of two numbers expressed in two s complement notation: S = A B; A = a 3 a 2 a a 0, B = b 3 b 2 b b 0, S = s 3 s 2 s s 0 We note that the positional values are (-2 3 ), (2 2 ), (2 ), and (2 0 ). The carry from lower three bits has a weight of (2 3 ) but the column into which it is going has a weight of (-2 3 ). Total of 8 cases. Four cases are a 3 b 3 = 00, 0, 0,. For each of these four cases, consider two cases: C 3 = a or a 0. 0a 2 a a 0 0a 2 a a 0 a 2 a a 0 a 2 a a 0 0b 2 b b 0 b 2 b b 0 0b 2 b b 0 b 2 b b 0 Hypothesis #: A number added to a negative number does not cause overflow. -2 3 0 a 2 a a 0 b 2 b b 0? s 2 s s 0 Since there is a already in in the MSB column, C4 follows C3. V = C3 () C4 = 0 If a carry C3 comes into the MSB column, it has a value of 2 3. And it will cancel the -2 3 resulting in a 0 for s3. Otherwise s3 will be a. Either way the values were preserved properly. 4 / 3 C Copyright 2006 Gandhi Puvvada
Hypothesis #2: A number added to another number causes overflow if C3 is true. -2 3 0 a 2 a a 0 0 b 2 b b 0? s 2 s s 0 C4 is surely a zero here. If C3 =, we have overflow. V = C3 () C4 = = overflow If a carry C3 comes into the MSB column, it has a value of 2 3. Since it results in making s3 a, which is interpreted as -2 3 it causes an overflow. Hypothesis #3: A negative number added to another negative number causes overflow if C3 is NOT true. -2 3 a 2 a a 0 b 2 b b 0 C4 is surely a one here. If C3 = 0, we have overflow. V = C3 () C4 =? s 2 s s 0 0 = overflow If a carry C3 comes into the MSB column, then s3 comes out to be a, which matches with the mathematical sum (2 3 ) (-2 3 ) (-2 3 ) = (-2 3 ). Absence of C3 results in overflow. 5 / 3 C Copyright 2006 Gandhi Puvvada
Subtraction A-B where A and B are expressed in two s complement notation: D = A - B; A = a 3 a 2 a a 0, B = b 3 b 2 b b 0, D = d 3 d 2 d d 0 (A - B) = (A B ), more precisely (A B 000). 8 cases. Four cases are a 3 b 3 = 00, 0, 0,. For each of these four cases, consider two cases: C 3 = a or a 0 while performing (A B 000). 0a 2 a a 0 0a 2 a a 0 a 2 a a 0 a 2 a a 0 0b 2 b b 0 b 2 b b 0 0b 2 b b 0 b 2 b b 0 An observation about 2 s complement notation. Given X ( or negative), we find the representation for -X by performing X, but... the 0000 and 000 are special cases. 0000 is considered to be a zero and there is no representation for a negative zero in 2 s complement notation, which is actually good! If you perform X, then you get ( 000) = 0000. Note that ( 000) contains a (-2 3 ) and also contains (2 2, 2, 2 0, and 2 0 ). Similarly 000 is also special. 000 is a -8, but if you negate it by doing X, you get (0 000) = 000. You get 000 again and it is NOT 8. In fact we caused an overflow in this addition. However please notice that the expression (0 000) as a whole still represents 8, as it has (2 2, 2, 2 0, and 2 0 ). 6 / 3 C Copyright 2006 Gandhi Puvvada
So, are we saying that we can NOT perform (A B ) for (A - B) if B is either 0000 or 000? No, no, we did not say that. In fact subtracting 0000 will never cause any problem. If you are subtracting 000, as long as you do not perform (A B ) in two steps [ (A) (B ) ] (i.e. as long as you add the three items all in one stroke), the procedure is still fine and there will not be any spurious overflow. Conclusion: Always perform (A B ) in one stroke. Hypothesis #: Subtracting a number from another number does not cause overflow. - 0 a 2 a a 0 0 b 2 b b 0? d 2 d d 0 0 a 2 a a 0 b 2 b b 0? d 2 d d 0 We already proved that the above addition cannot cause overflow. B can be 0000 without any problem. Since there is a already in in the MSB column, C4 follows C3. V = C3 () C4 = 0 7 / 3 C Copyright 2006 Gandhi Puvvada
Hypothesis #2: Subtracting a negative number from another negative number does not cause overflow. - a 2 a a 0 b 2 b b 0? d 2 d d 0 a 2 a a 0 0 b 2 b b 0? d 2 d d 0 We already proved that the above addition cannot cause overflow. B can be 0000 without any problem. Hypothesis #3: Subtracting a number from a negative number can cause overflow. - Since there is a already in in the MSB column, C4 follows C3. V = C3 () C4 = 0 a 2 a a 0 0 b 2 b b 0? d 2 d d 0-2 3 0 = overflow C4 is surely a one here. If C3 = 0, we have overflow. V = C3 () C4 = a 2 a a 0 b 2 b b 0? d 2 d d 0 As stated before, if a carry C3 comes into the MSB column, then d3 comes out to be a, which matches with the mathematical sum (2 3 ) (-2 3 ) (-2 3 ) = (-2 3 ). Absence of C3 results in overflow. 8 / 3 C Copyright 2006 Gandhi Puvvada
Hypothesis #4: Subtracting a negative number from a number can cause overflow. - 0 a 2 a a 0 b 2 b b 0? d 2 d d 0 C4 is surely a zero here. If C3 =, we have overflow. V = C3 () C4 = -2 3 = overflow 0 a 2 a a 0 0 b 2 b b 0? d 2 d d 0 As stated before, if a carry C3 comes into the MSB column, then d3 comes out to be a, which cause overflow Presence of C3 results in overflow. Conclusion: Whether you subtract or add, the V = C3 () C4 provides overflow signal. 9 / 3 C Copyright 2006 Gandhi Puvvada
Addition of two unsigned numbers: C4 = indicates overflow in addition. Subtraction in unsigned numbers: 2 3 Error point: C bit is set 4 0 00 5 0 0 0000 000 000 00 UNSIGNED 000 2 3 4 Since negative numbers can not be represented in unsigned system, when the correct result is expected to be negative, there is an overflow. If A is lower than B in the subtraction A - B, then we have an overflow. 0 0 00 00 9 000 8 0 00 00 7 6 5 To perform A - B, let us perform A (2 4 - B). A (2 4 - B) = A ( - B ) = A B If (A B ) produces C4, then we know that the (2 4 ) added by us was not really needed for subtracting. It means that A is higher than or equal to B. If C4 was not produced, then A is less than B. C4 = 0 indicates overflow in subtraction. We call the above C4 Raw Carry. In many processors, it (the raw carry) is flipped for subtraction and recorded in the Flag Register (also called CCR (Condition Code Register), PSW (Processor Status Word). etc.). The processor designers do this (flip the raw carry) because it is easy for the programmers to remember one simple rule, Carry flag is the overflow indicator for unsigned addition or subtraction. When it is set, unsigned overflow has occurred. We call this Processed Carry to distinguish from the Raw Carry. 0 / 3 C Copyright 2006 Gandhi Puvvada
If you try to perform addition and subtraction with each possible pair of numbers in the range of 0000 to, is it possible to encounter all combinations of overflows (signed overflow and unsigned overflow)? Yes! Try to avoid using (though not necessary) 0000 and 000 numbers for A and B. 4-bit A 4-bit B UNSIGNED 4 2 3 0 0 00 0 Operation 4-bit Result Result / if numbers are treated as signed numbers Addition Addition Addition Addition Subtraction Subtraction Subtraction Subtraction 5 0 9 0 0000 000 2 000 3 00 000 4 00 5 Smallest (0) to Largest (5) 00 00 00 000 0 8 7 6 Result / if numbers are treated as unsigned numbers - 0-2 0000 000 2 0-3 000 0 00 Smallest (-8) 3-4 - 5 00 0 00 to Largest (7) 000 00 00 4 5-6 00 000 0 6-7 - 8 7 V Raw Carry C4 2-4 3-3 - 5 Error point: C bit is set 0-2 - 6 4 0 00 0 00 9 5 0 LARGER mag. 0 8 7 0000 000 000 00 SMALLER mag. 000 00 00 00 00 000 0 0 0-0 - 7 0-8 7 UNSIGNED 0000 000 000 SMALLER mag. LARGER mag. 00 000 00 00 00 00 000 0 6 6 2 2 5 3 5 4 SIGNED 3 4 Error Point: V bit is set / 3 C Copyright 2006 Gandhi Puvvada
Special note about avoiding spurious overflow while performing (A - B) by doing (A B ): Subtraction in unsigned numbers: We said that the absence of the Raw Carry C4 at the end of the subtraction is an indication of the overflow. When we said end we meant at the end of the addition of the 3 items in (A B ). If you perform this addition in two parts (add two of the three items together first, and then add the third, then there is a problem if you try to subtract a zero. A - B = a3 a2 a a0-0 0 0 0 = (a3 a2 a a0 ) If you perform the addition of the three items in the last step in one stroke, then C4 will definitely be true because => C4 = ). However if you perform the addition in two steps as [ a3 a2 a a0 ( ) ], then in the second step you will not find Raw Carry C4 as and you could wrongly interpret that there was an unsigned subtraction overflow. Subtracting other numbers (other than 0000) including the number 000 do not pose any problems whether you perform the subtraction in one step or in two steps [(B ) A]. However, if you perform the addition in two steps grouping the three terms differently, then you can run into the same problem. Examples: [(A ) B ] or [(A B ) ] Conclusion: always perform the addition of the three items in one stroke. 2 / 3 C Copyright 2006 Gandhi Puvvada
Subtraction in signed numbers: Here again we need to perform the addition of the three items (A B ) in on stroke. If you perform the addition in two strokes in any one of the three possible ways below, you can be causing spurious overflow(s). [(B ) A ] -- case [(A ) B ] -- case 2 [(A B ) ] -- case 3 Note that the is considered as a one, i.e. 000. To provide examples where we run into spurious overflow problem, basically we need to make the first step to cause signed overflow, which would have been averted had we added the three items together in one stroke. For (something ) to cause overflow, the something should be the largest number 0. Also for that overflow to be averted the third item shall be negative. So an example for case or case 2 is [(0 ) ] [(B ) A ] Consider A = (-) = and B = (-8) = 000. Then A - B = (7) [(A ) B ] Consider A = (7) =0 and B = (0) = 0000. Then A - B = (7) [(A B ) ] Consider A = (-4) =00 and B = (4) = 000. Then A - B = (-8) Note that when there is a spurious overflow in the first step, then there will be a spurious overflow in the second step also. 3 / 3 C Copyright 2006 Gandhi Puvvada