numeric_limits<int>
tells us that the possible values for an
int
go from
-2,147,483,648 to
+2,147,483,647.
That means there are
4,294,967,296
possible values in this range, including 0.
Why exactly are there 4,294,967,296 of them?
There are 2 = 21
possible values that can be spelled out with 1 bit:
0
1
There are 4 = 22
possible values that can be spelled out with 2 bits:
00
01
10
11
There are 8 = 23
possible values that can be spelled out with 3 bits:
000
001
010
011
100
101
110
111
There are 16 = 24
possible values that can be spelled out with 4 bits:
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
There are 32 = 25
possible values that can be spelled out with 5 bits:
00000
00001
00010
00011
00100
00101
00110
00111
01000
01001
01010
01011
01100
01101
01110
01111
10000
10001
10010
10011
10100
10101
10110
10111
11000
11001
11010
11011
11100
11101
11110
11111
Note that the number of possible values is always an even number,
and that it doubles each time.
Please satisfy yourself that the general rule is:
there are
2n
possible values that can be spelled out with
n
bits.
For example,
there are
25 =
32
possible values that can be spelled out with 5 bits,
and
232 =
4,294,967,296
possible values that can be spelled out with 32 bits.
Since an
int
variable
on
storm.cis.fordham.edu
can hold any one of 4,294,967,296 possible values,
this means that an
int
variable
on
storm.cis.fordham.edu
must occupy at least 32 bits of memory.
32 bits = 4 bytes, since each byte is 8 bits.
Why does this range of 4,294,967,296 possible values extend very slightly farther in the negative direction than in the positive direction? Why doesn’t the range extend equally far in both directions?
It would be impossible for a range of 4,294,967,296 possible values to extend equally far in both directions, because 4,294,967,296 is an even number. Any range that extended equally far in both directions would have to be a range of an odd number of possible values. For example, a range that extends from -10 to +10 would have 21 values.