13

Sorry if this is a really basic question, but why is there a minus one for the positive side?

Does it have to do with the zero being stored or something? I thought computing the highest possible decimal number for binary would just be to add the powers of two up, like for a 3 bit unsigned it would be

1*2^0 + 1*2^1 + 1*2^2 = 7

Shouldn't the same rule apply for java integers? Thanks

Lucas Ou-Yang
  • 5,505
  • 13
  • 43
  • 62
  • 1
    Because of [two's complement](http://en.wikipedia.org/wiki/Two's_complement); see related question: http://stackoverflow.com/questions/3809044/how-many-values-can-be-represented-with-9-bits/3809058#3809058 – NullUserException Oct 12 '12 at 20:25
  • It's unclear what you are asking. We do "just add the powers of two up", in your example you are doing 1+2+ 4 which is same as 2^3 - 1 – Ivan Anatolievich Jul 31 '18 at 10:43

7 Answers7

15

Because Java can support max signed int as 0x7fffffff which is 2^31-1.

2^31 = 0x80000000 is negative so Positive is 2^31-1

Binary level comparasion would be:

10000000000000000000000000000000  --> 2147483648 --> 2^31
01111111111111111111111111111111  --> 2147483647 --> 2^31 -1
^ Sign bit
Yogev Levy
  • 325
  • 3
  • 16
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • Thanks, this makes it quite a bit more clear – Lucas Ou-Yang Oct 12 '12 at 20:37
  • 2
    "Signed bit" should be "Sign bit" – Roland Aug 23 '16 at 07:48
  • 1
    This is wrong. You have 32 bits, the 1 bit is used as sign bit, so you are left with 31 bits, but the question is not about the number of bits. The question is why the maximum is 2^31-1 and not the 2^31. This is because 2^31 is how many numbers you have, one of those numbers is 0 so the highest number becomes 2^31-1. – Chewie Jul 04 '21 at 22:49
8

The same rule does apply... 7 is 2^3 - 1. And yes, it's because of the 0. :)

In contrast, negatives go to -(2^31)

So there's 2^31 negative numbers, one 0, and 2^31-1 strict positives, which add to...

2^31 + 1 + 2^31 - 1 = 2 * 2^31 = 2^32
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
6

It's because of the convenience of two's complement (which avoids storing two zeros), and Java stores numbers using that rapresentation. Take a look here.

Jack
  • 131,802
  • 30
  • 241
  • 343
5

There are 2^31 non-negative numbers ranging from 0 to 2^31-1. So, yes, zero is stored as an integer, too. And also, there are 2^31 negative numbers ranging from -2^31 to -1.

RGO
  • 4,586
  • 3
  • 26
  • 40
2

It has to split up 2^32.
1/2 are negative.
0 counts with the positive.
In math 0 is neither negative nor positive.
It is consistent in .NET and MSSQL.

If you notice the set that does not include negatives is called unsigned.
It contains 0 and would not proper to call it positive.
Since the binary world starts at 0 it is kind of treated as positive.
The answer from Jack (+1) has why.

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • 1
    0 is not considered positive, it's inserted between the positive numbers in two's complement just for a matter of convenience. – Jack Oct 12 '12 at 20:29
  • @NullUserException You are correct I just Wiki'd it and updated my answer. – paparazzo Oct 12 '12 at 20:30
1

If you have n bits you have 2^(n-1) negative numbers (as the top bit is a 1) and 2^(n-1) non-negative numbers. As zero is a non-negative number you have up to 2^(n-1)-1 positive numbers which is also the maximum.

Note: there is no positive for the most negative number so

-Integer.MIN_VALUE == Integer.MIN_VALUE
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Java integers are signed quantities, so one bit is reserved for the sign, leaving 31 bits for the value.

Brenda Bell
  • 1,139
  • 8
  • 10