Why does the range of int has a minus 1?

2019-03-20 13:35发布

问题:

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 ^ (31)

Why do some people say its 2^31 - 1 though?

Thanks!

回答1:

Because the counting starts from 0

And the range of int is 2,147,483,647 and 2^32 which is 2,147,483,648. hence we subtract 1

Also the loss of 1 bit is for the positive and negative sign

Check this interestinf wiki article on Integers:-

The most common representation of a positive integer is a string of bits, using the binary numeral system. The order of the memory bytes storing the bits varies; see endianness. The width or precision of an integral type is the number of bits in its representation. An integral type with n bits can encode 2n numbers; for example an unsigned type typically represents the non-negative values 0 through 2n−1. Other encodings of integer values to bit patterns are sometimes used, for example Binary-coded decimal or Gray code, or as printed character codes such as ASCII.

There are four well-known ways to represent signed numbers in a binary computing system. The most common is two's complement, which allows a signed integral type with n bits to represent numbers from −2(n−1) through 2(n−1)−1. Two's complement arithmetic is convenient because there is a perfect one-to-one correspondence between representations and values (in particular, no separate +0 and −0), and because addition, subtraction and multiplication do not need to distinguish between signed and unsigned types. Other possibilities include offset binary, sign-magnitude, and ones' complement.



回答2:

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).



回答3:

int is a signed data type. The first bit represents the sign, followed by bits for the value. If the sign bit is 0, the value is simply the sum of all bits set to 1 ( to the power of 2).

e.g. 0...00101 is 20 + 22 = 5

if the first bit is 1, the value is -232 + the sum of all bits set to 1 (to the power of 2).

e.g. 1...111100 is -232 + 231 + 230 + ... + 22 = -4

all 0 will this result in zero.

When you calculate after, you will see that any number between (and including) the range - 231 and 20 + ... + 231 = 232 - 1 can be created with those 32 bits.



回答4:

232-1 is not same as 232 - 1 (as 0 is included in the range, we subtract 1)

For your understanding, let us replace by small number 4 instead of 32

24-1 = 8 whereas 24-1 = 16-1 = 15.

Hope this helps!



回答5:

Since integer is 32 bit. It could store total 2^32 values. So an integer ranges from -2^31 to 2^31-1 giving a total of 2^32 values(2^31 values in the negative range+2^31 values in positive range including 0).However, the first bit(the most significant bit) is reserved for the sign of the integer. Again u need to understand how negative integers are stored.They are stored in 2's complement form, So -9 will be stored as 2's complement of 9. So 9 is stored in 32 bit system as 0000 0000 0000 0000 0000 0000 0000 1001 and -9 will be stored as 1111 1111 1111 1111 1111 1111 1111 0111 (2's complement of 9).

Again due to some arithmetic operation on an integer, if it happens to exceed the maximum value(2^31-1) then it will recycle to the negative values. So if you add 1 to 2^31-1 it will give you -2^31.