开发者

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 ?


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


What do you mean?

It will typically mean -127 to 128 inclusive, so both -127 and 128 are themselves valid values.


Normally, the ranges of the values indicates how much memory they use, and they are normally designed to ocuppy full bytes. In your case (-127 to 128), this type will occupy 1 byte, which can have 256 different values.

So, you have 127 negative values, 128 positive values, and the 0 value. 127 + 128 + 1 = 256.

So, the values -127 and 128 are included in the range.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜