Hello all!

I am having difficulty understanding why the range of signed integers is from -128 to +127? Assuming the size of the register is 8 bits and the leftmost bit is reserved for sign shouldn't the size be -127 to +127. I figured that for the negative values the 8th bit will have a one and if you want the maximum value, the remaining bits will too be set to 1, thus the limit for the negative values will be 11111111 = -127. I can understand why the max possible value for positive integers is +127 as the value in the register will be 0111111 = +127. Do let me know where I am going wrong in understanding such a simple thing. Thank you all!

Assuming the size of the register is 8 bits and the leftmost bit is reserved for sign shouldn't the size be -127 to +127.

It is. Well, that's the required minimum size defined in the C standard. However, the range can be extended by any implementation. Particularly, the extra bit of range on the negative side is due to how two's complement works with signed values. Different sign representations have different ranges, but two's complement is the most common, and they all meet the minimum requirement.

Your best bet for portable code is never to assume that your range exceeds [-127,127] for any 8 bit quantity.

This is why we have such system defined macros as INT_MIN and INT_MAX, etc. Those will be set appropriately for the architecture of your system, 2's complement or otherwise. FWIW, for an 8 bit integer (char) value, you would use the SCHAR_MIN and SCHAR_MAX macros. Also, for Intel class processors, SCHAR_MIN == -128 and SCHAR_MAX == 127. The high-order bit is the sign bit which is why the range is -128 through 127 and not + or - 127.

Hey thanks a lot to both of you! Really cleared the stuff up for me!

This article has been dead for over six months. Start a new discussion instead.