I was looking into 32-bit and 64-bit. I noticed that the range of integer values that can stored in 32 bits is ±4,294,967,295
but the Java int
is also 32-bit (If I am not mistaken) and it stores values up to ±2 147 483 648
. Same thing for long
, it stores values from 0 to ±2^63
but 64-bit stores ±2^64
values. How come these values are different?
-
You can use the signed primitives of Java to store unsigned values. Java doesn't provide support for this but with a few (often minor work-arounds) it works just fine. Obviously in languages which support unsigned types, you don't need those work-arounds. – Peter Lawrey Dec 04 '11 at 20:14
-
To store 2^64 negative values and 2^64 positive values you need something which stores 2*2^64 or 2^65 or 65-bits. To store 2^63 negative and non-negative values you need 2*2^63 or 64-bits. – Peter Lawrey Dec 04 '11 at 21:06
3 Answers
Integers in Java are signed, so one bit is reserved to represent whether the number is positive or negative. The representation is called "two's complement notation." With this approach, the maximum positive value represented by n bits is given by
(2 ^ (n - 1)) - 1
and the corresponding minimum negative value is given by
-(2 ^ (n - 1))
The "off-by-one" aspect to the positive and negative bounds is due to zero. Zero takes up a slot, leaving an even number of negative numbers and an odd number of positive numbers. If you picture the represented values as marks on a circle—like hours on a clock face—you'll see that zero belongs more to the positive range than the negative range. In other words, if you count zero as sort of positive, you'll find more symmetry in the positive and negative value ranges.
To learn this representation, start small. Take, say, three bits and write out all the numbers that can be represented:
- 0
- 1
- 2
- 3
- -4
- -3
- -2
- -1
Can you write the three-bit sequence that defines each of those numbers? Once you understand how to do that, try it with one more bit. From there, you imagine how it extends up to 32 or 64 bits.
That sequence forms a "wheel," where each is formed by adding one to the previous, with noted wraparound from 3 to -4. That wraparound effect (which can also occur with subtraction) is called "modulo arithemetic."

- 14,999
- 2
- 48
- 58
-
1Personally I always thought that thinking about it as a just a polynomial `b_{n-1}*2^{n-1} + b_{n-2}*2^{n-2} +.. + b_{0}*2^0` with b's either 0 or 1 and n = word width. The difference between signed and unsigned is then just whether the first term is negative or not. – Voo Dec 04 '11 at 20:58
-
1Nice explanation, @Voo. An easier to grasp but less formal definition simply says that if the high bit is one, then the value is, say, -128 for an 8-bit number, with the meaning of the rest of the bits unchanged. That explains why `11111111` is -1; it's equivalent to `01111111`, or 127, plus -128. Again, the same as your definition, but *implying* less possibly confusing multiplication. By the way, I appreciate the TeX notation. – seh Dec 04 '11 at 21:23
-
@seh Yup, you're basically describing the formal definition in an informal way - certainly simpler to understand for anyone but the most math-inclined people.. that's why I'd make a horrible teacher really ;) – Voo Dec 05 '11 at 00:47
In 32 bit you can store 2^32 values. If you call these values 0 to 4294967295 or -2147483648 to +2147483647 is up to you. This difference is called "signed type" versus "unsigned type". The language Java supports only signed types for int
. Other languages have different types for an unsigned 32bit type.
NO laguage will have a 32bit type for ±4294967295, because the "-" part would require another bit.

- 63,967
- 15
- 92
- 126
-
If you want the ordering to work "out of the box," you have to go with the signed range. – yshavit Dec 04 '11 at 20:06