cbinarybit-manipulationtwos-complementones-complement

Why is 128 in one and two's complement using 8 bits overflow?


Suppose I want to represent 128 in one and two's complement using 8 bits, no sign bit

Wouldn't that be:

One's complement: 0111 1111

Two's complement: 0111 1110

No overflow

But the correct answer is:

One's complement: 0111 1111

Two's complement: 0111 1111

Overflow

Additional Question:

How come 1 in one's and two's complement is 0000 0001 and 0000 0001 respectively. How come you don't flip the bits like we did with 128?


Solution

  • One's and Two's Complements are both ways to represent signed integers.

    For One's Complement Representation:

    For Two's Complement Representation:

    Therefore, 128 overflows in both instances because the binary representation of 128 is 1000 0000 which in ones complement represents -127 and in twos complement represents -128. In order to be able to represent 128 in both ones and twos complement you would need 9 bits and it would be represented as 0 1000 0000.