Reputation: 23
I have trouble understanding when overflow occurs in unsinged addition and subtraction. For example,
1 1 1 1 0 0 0 0
+ 0 0 1 1 1 0 0 0
__________________
0 0 1 0 1 0 0 0
Because of the 1 in the MSB, it results as overflow. I understand this problem, but when it gets to subtraction, I have trouble determining when it is overflow
For example,
0 0 0 0 0 0 0 1
- 0 0 0 0 0 0 1 1
(After applying 2's complement)
0 0 0 0 0 0 0 1
+ 1 1 1 1 1 1 0 1
1 1 1 1 1 1 1 0
Therefore the result should be "no overflow" because there is no 1 carry out in the end. However, the answer says "overflow". Could you please tell me why?
Upvotes: 1
Views: 853
Reputation: 60908
I'd probably call this underflow not overflow.
Think about it like this. You are trying to compute x − y. Using two's complement you are implementing this as x + (2N − y) = 2N + (x − y). So the result will only correctly represent (x − y) if there is an overflow carry bit 2N that fell off the left. Otherwise the result you get is the two-s complement representation of a negative subtraction result.
Upvotes: 1