-3

I have a question in java about integer overflow.

When I search max integer and surfed integer for that binary, I got this "0111 1111 1111 1111 1111 1111 1111 1111" and also when I use the value 3, I got the binary as "0000 0000 0000 0000 0000 0000 0000 0011". How is that possible because 2^0+2^1=3? Fine but for the maximum value, 2^31 is 2147483648 and it reached the maximum value for int for just 2^31, we still not add the remaining 2^30 +2^29 and so on? So, how all the bits of the maximum integer number becomes "1" except the MSB?

I want to know that full working, how the things work clearly.

Edit : How some people calculate without the 2^31 in there calculation like 2^0+2^1+....2^30 (not including the 2^31) why because it has 32 bits , 1 bit is for positive or negative, so remaining 31 bits , we have to calculate the 31 bits right ?why only 30 bit?

  • 2
    The max integer is 2147483647, which *is* 2^30+2^29+2^28+... – luk2302 May 04 '23 at 10:51
  • 1
    "..also when I use the value 3, I got the binary as "0000 0000 0000 0000 0000 0000 0000 0011". How is that possible because 2^0+2^1=3?" <- What is that even supposed to mean? What are you asking here? – OH GOD SPIDERS May 04 '23 at 11:04
  • Why only 30 bits??? The last bit is for power 0, 2^0 till 2^30 are 31 summands / bits. – luk2302 May 05 '23 at 06:23

1 Answers1

2

The negative numbers also need a slot. Computers just have ones and zeroes; there is no 'minus sign'. So we 'encode' negative numbers with zeroes and ones. The first bit is the sign bit; if it is a 1, the number is actually negative. Hence why the largest (non-negative!) number is 0111 1111 1111 1111 1111 1111 1111 1111. In decimal it is, as you would expect, 2^0+2^1+2^2+.....2^30. Which is the same as 2^31-1, which is 2147483647. And indeed it is:

System.out.println(Integer.MAX_VALUE); // prints 2147483647.
System.out.println(0b0111_1111_1111_1111_1111_1111_1111_1111); // same
int v = 0;
for (int i = 0; i < 31; i++) {
  int z = (int) Math.pow(2, i);
  v += z;
}
System.out.println(v); // same

The '0' also has to go somewhere, and it is all zero bits. That means a bit of an intriguing detail: There are, of course, as many numbers that starts with a 1-bit as with a 0-bit, but the 0-bit space (our positive numbers) needs to allocate one sequence for zero, but the negative space does not. Hence, the largest negative number you can represent is actually one higher: It's -2^31:

System.out.println(Integer.MIN_VALUE);

This prints -2147483648.

Negative numbers are represented in 2's complement. Basically, 'in reverse'. 1000 0000 0000 0000 0000 0000 0000 0000 is in fact -2147483648, and 1111 1111 1111 1111 1111 1111 1111 1111 is -1. You can read e.g. wikipedia about all the details of two's complement, but one massive advantage is that the + and - operations (as well as increment/decrement-by-one operations) work identically regardless of whether you treat the bits as unsigned (negative numbers are not possible, can represent 0 up to 2^32-1), or as signed (negative numbers ARE possible, can represent -2^31 to +2^31-1). Now 'is it signed or unsigned' is math-wise irrelevant, and that saves instructions.

For a quick moment of enlightenment: Take -1 in two's complement (in bits: 32x 1), and add 1 to it. This trivially results in a 1 followed by thirty-two zeroes, and as computers just lop off the excess, that's just thirty-two zero bits. Which represents zero. And -1 incremented by 1 is.. indeed zero. Neat!

2's complement boils down to 'flip all bits and add one to it', e.g:

  • 5 = 0000.....0101
  • -5 thus becomes: 1111....1011.
  • reversing that operation produces 000...0101 again.
rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Hence why the largest (non-negative!) number is 0111 1111 1111 1111 1111 1111 1111 1111. In decimal it is, as you would expect, 2^0+2^1+2^2+.....2^30. Which is the same as 2^31-1, which is 2147483647 why ? Stopped on 2^30 instead of 2^31 because it have 32 bit right , the 32 nd bit is for positive or negative, then why not include 31 bit for that calculation? – Tholkappiar May 04 '23 at 11:23
  • 1
    @Tholkappiar 2^0 represents the 1st bit, 2^1 is the 2nd bit ... and 2^30 is the 31st bit. 2^31 is the 32nd bit which represents the negative sign. – Guy Incognito May 04 '23 at 11:54
  • Ok now I understand the thing , which ruined my mind..so people are calling the 31st bit as a 32nd bit because it has 32 chars right ? – Tholkappiar May 04 '23 at 12:02
  • 1
    No, they call it the 32nd bit because it *is* the 32nd bit – Guy Incognito May 04 '23 at 12:19
  • 2
    @Tholkappiar: How many numbers are this `0`, `1`? Its 2 numbers. The first one is `0` and the second one `1`. `0`, `1` and `2` Is three numbers with `0` the first and `2` the third. And in the same way 0-31 is 32 numbers/bits with `0` being the first and `31` being bit number 32. You main thinking error is that you constantly seem to not want to start counting at `2^0` despite the fact that you include it in e very example calculation you post. `2^0` is the first bit and not the "zeroest" bit (Whatever that would be) and therfor `2^31` is bit number 32. – OH GOD SPIDERS May 04 '23 at 12:32
  • It's a 32 bit number, the maximum number is 2^31 because it's starts from 2^0 , so 2^32 not included , right ? – Tholkappiar May 04 '23 at 12:39
  • 3
    2^32 is not representable by 32 bits, period. Because 2^32 represents the 33rd bit. At some point you're going to have to look at this sentence intently: __2^0 is representing the FIRST BIT__ and keep looking at it until you understand that one sentence. – rzwitserloot May 04 '23 at 13:37