views:

39

answers:

2

Why is it that an arithmetic overflow cannot occur when adding an positive and a negative number using two's complement. If you could please provide an example with 8-bit signed integers (bytes).

+1  A: 

Assume that you have a positive number A, and a negative number B. Their sum is S. Then:

S <= A && S >= B

Their sum would be somewhere in the middle. Note that there would be a carry, but that is not an overflow(incorrect sum).

AraK
+2  A: 

This ... kind of sounds like homework. Did you mean to use the 'homework' tag?

The reason you can't overflow is because adding a positive x and a negative number y will produce a value z such that abs(z) < abs(x) and abs(z) < abs(y). Since x and y could be represented without overflow, and z is closer to zero than either one, z can also be represented without overflow.

Any pair of positive and negative numbers form an example.

Borealid
Thank you very much, this is just what I was looking for. And no, this is not homework, I am writing an 8086 emulator.
X-N2O