Why is it that an arithmetic overflow cannot occur wh开发者_如何学Goen adding an positive and a negative number using two's complement. If you could please provide an example with 8-bit signed integers (bytes).
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.
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).
精彩评论