As you are probably aware, int
s are stored internally in binary. Typically an int
contains 32 bits, but in some environments might contain 16 or 64 bits (or even a different number, usually but not necessarily a power of two).
But for this example, let's look at 4-bit integers. Tiny, but useful for illustration purposes.
Since there are four bits in such an integer, it can assume one of 16 values; 16 is two to the fourth power, or 2 times 2 times 2 times 2. What are those values? The answer depends on whether this integer is a signed int
or an unsigned int
. With an unsigned int
, the value is never negative; there is no sign associated with the value. Here are the 16 possible values of a four-bit unsigned int
:
bits value
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 10
1011 11
1100 12
1101 13
1110 14
1111 15
... and Here are the 16 possible values of a four-bit signed int
:
bits value
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 -8
1001 -7
1010 -6
1011 -5
1100 -4
1101 -3
1110 -2
1111 -1
As you can see, for signed int
s the most significant bit is 1
if and only if the number is negative. That is why, for signed int
s, this bit is known as the "sign bit".