Why does the range of int has a minus 1?

后端 未结 5 1735
夕颜
夕颜 2021-02-15 11:17

I read that the range of an int is dependent on a byte.

So taking int to be 4 bytes long, thats 4 * 8 bits = 32 bits.

So the range should be : 2 ^ (32-1) = 2 ^ (

5条回答
  •  [愿得一人]
    2021-02-15 11:48

    You mean 232-1, NOT 232-1.

    But your question is about why people use 231. The loss of a whole bit is if the int is a signed one. You lose the first bit to indicate if the number is positive or negative.

    A signed int (32 bit) ranges from -2,147,483,648 to +2,147,483,647. An unsigned int (32 bit) ranges from 0 to 4,294,967,295 (which is 232 -1).

提交回复
热议问题