Search code examples
programming-languagestypes

Query on integer range in a Programming Language


All,

This might be a very silly question, but in a Programming lang X where the int range is -127 to +128, does this value refer to the actual value -127 and +128 ?


Solution

  • It refers to an 8-bit signed integer, where the high bit is used to determine whether it's negative or not:

    01111111 = 127
    00000001 = 1
    00000000 = 0
    11111111 = -1
    11111110 = -2
    10000001 = -127
    10000000 = -128 or +128 or even -0, depending on the language
    

    See: http://en.wikipedia.org/wiki/Two%27s_complement