Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Why is 2,147,483,647 the max int value?

807607Oct 5 2006 — edited Oct 5 2006
Can someone provide me with a detailed explanation of why 2,147,483,647 is the maximum integer value in Java?

I'm writing a report and need to know and i can't find an explanation anywhere.

Thanks a lot

Comments

807607
An int value is a 32-bit number, and that's the largest value that can be represented in 32-bit two's complement.

Binary numeral system (Wikipedia)
Two's complement (Wikipedia)
Cat and Mouse Games with Bits

If you still have trouble understanding after reading the links, please ask, but the links contain the full, detailed explanation you need.

~
807607
Can someone provide me with a detailed explanation of
why 2,147,483,647 is the maximum integer value in
Java?

I'm writing a report and need to know and i can't
find an explanation anywhere.

Thanks a lot
Because that's how high Java can count using 32 toes and fingers.
masijade
Can someone provide me with a detailed explanation
of
why 2,147,483,647 is the maximum integer value in
Java?

I'm writing a report and need to know and i can't
find an explanation anywhere.

Thanks a lot
Because that's how high Java can count using 32 toes
and fingers.
Really? I only have 21. Wait a minute! That's neither a toe nor a finger!
807607
Ok I read through the material and Im still a little confused, although I think I get the gist.

This what i've come up with,
The integer values in Java are 32-bit Two's complement encoded
numbers and the highest possible value for this system is
2,147,483,647. The value 2,147,483,648 or 2n-1 where n is 32 is
negated under two's complement representation and becomes the
-2n-1s place. It is the most significant bit, meaning it has the
greatest value. In Two's compliment the most significant bit is
becomes the "sign bit" and indicates the sign of the number. If the
sign bit is 0 then largest number that can be stored in the remaing
31 bits is (2n-1-1) or 232-1-1 = 231-1 = 2,147,483,648 -1 =
2,147,483,647.
JosAH

Ok I read through the material and Im still a little
confused, although I think I get the gist.

[ snip ]

Yep, you got it alright. Suppose there were only three bits in a word.
The following table would describe it all then:

binary unsigned 2s-complement 1s-complement
-------------------------------------------
000    0         0             0
001    1         1             1
010    2         2             2
011    3         3             3
100    4        -4            -3
101    5        -3            -2
110    6        -2            -1
111    7        -1            -0

You can forget all about the last column if you like.

kind regards,

Jos

796440
What in particular are you still confused about?

Your explanation looks pretty much correct. Note that the high-order bit isn't strictly a "sign bit." What I mean is, if it were "strictly a sign bit," then -1 would be the same as+1 with a one in the first bit. -1 would be 0x10000001. But that's not that case. (I think that's one's comlement, actually).

-1 = 0xFFFFFFFF
-2 = 0xFFFFFFFE
most negative number = 0x10000000
most positive number = 0x7FFFFFFF

Note that if you subtract one from the most negatie number, you wrap around to the most positive, and if you add one to -1, you get 0 (ignoring any carry/borrow).
807607
...if it were "strictly a sign bit," then -1
would be the same as+1 with a one in the first bit.
-1 would be 0x10000001. But that's not that case. (I
think that's one's comlement, actually).
One's complement is simply the inverse of the positive number. A representation with a pure sign bit is... well, just a sign bit... no "complement" of any sort.
796440
...if it were "strictly a sign bit," then -1
would be the same as+1 with a one in the first
bit.
-1 would be 0x10000001. But that's not that case.
(I
think that's one's comlement, actually).
One's complement is simply the inverse of the
positive number.
Ah, yes. I knew that didn't sound right. Thanks for setting me straight.
1 - 8
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 2 2006
Added on Oct 5 2006
8 comments
71,460 views