Search code examples
numbersoverflowmathsigned

Arithmetic Overflow


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).


Solution

  • 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.