Reputation: 1966
Say I have 21 - 10 = 10 in decimal.
Signed method:
10101
- 1010
-----
1011 = 11 (in decimal)
So far so good,
Now if I decide to use two's complement
(0)
10101 (21)
+ 10110 (-10)
-----
(1) 01011 = (11)
Here it shows I have a carry in of zero and carry out of one. If I follow the rules of underflow and overflow, this should be considered as an underflow and should not be getting the correct answer in the end.
However, if I decide to add one more bit to work with:
(1)
010101 (21)
+ 110110 (-10)
-----
(1) 001011 = (11)
In this case, I get the correct answer and no overflow.
I cannot seem to figure out what's happening here and why is it that I'm getting the correct answer for each of these cases in two's complement.
Any clarification would help!
Upvotes: 1
Views: 557
Reputation: 5395
With only 5 bits, 10101 is the representation of -11. You're getting the "right" answer because what you're actually calculating is -11 + -10 and getting 11, which is the incorrect answer, consistent with the rules of over/underflow.
Upvotes: 2
Reputation: 61949
In 5 bits, 10101 is not 21!
The most significant bit is set, so it is some negative number!
Upvotes: 2