Search code examples
bit-manipulationbittwos-complementones-complement

Why is that magnitude of largest negative number represented in base -2, is twice as large as largest positive number represented?


Here they mention that " If the word has an even number of bits, the magnitude of the largest negative number that can be represented is twice as large as the largest positive number that can be represented, and vice versa if the word has an odd number of bits."

After re-reading several times, I still don't get it. Could you explain with some example? And Also for the vice versa part.


Solution

  • As it says in the link you posted, "The rightmost bit represents (−2)^0 = +1, the next bit represents (−2)^1 = −2, the next bit (−2)^2 = +4 and so on, with alternating sign."

    If the bits alternate in sign, and the first bit is a positive number, then every even bit will result in a negative number. If this even left-most bit is set to 0, the number would be positive, however, the absolute value would be half as much as the previous number.

    For example:

    0101 = 5 because it's (-2)^0 + (-2)^2 = +1+4

    1010 = -10 because it's (-2)^1 + (-2)^3 = -2-8

    However, if we were limited to 3 bits, we would have

    010 = -2 because it's (-2)^1 = -2

    101 = 5 because it's (-2)^0 + (-2)^2 = 1 + 4 = 5

    Essentially, in base-2 the largest negative number you can reach is one where every even bit is set to 1, and every odd bit is set to 0. And the highest positive number is one where the opposite is true.

    If the total number of bits allowed is even, then setting the left most even bit to 1 would yield a negative number at least twice as large as the largest positive number (as any positive number would leave that left most bit set to 0). And the opposite is true with an odd number of bits.