1

I have the following snippet of code

public class DN1{
  public static void main(String argv[]){
    int a = 869;
    int b = 85;
    for(int i = 0; i < a; i++){
      b += b;
    }
    System.out.println(b);
  }
}

This code clearly overflows at i = 24

I was wondering why after a couple of loops b is set to 0. What is this overflow behaviour and how exactly does it work in Java?

I expected the output to be variable between -2^31 to +2^31 but it's not, why is that?

Nik Vinter
  • 145
  • 10

2 Answers2

2

To be more precise, the output is in the range you specified. 0 qualifies as being between Integer.MIN_VALUE and Integer.MAX_VALUE.

Modifying your code a bit you can take a look at what's going on and print the actual binary

int a = 869;
int b = 85;
for(int i = 0; i < a; i++){
    System.out.printf(
            "Iteration %d: b = %d; b+b = %d; bin(b) = %s; bin(b+b) = %s%n",
            i, b, (b+b), Integer.toBinaryString(b), Integer.toBinaryString(b+b));
            b += b;
}
System.out.println(b);

Which gives the following output

Iteration 0: b = 85; b+b = 170; bin(b) = 1010101; bin(b+b) = 10101010
Iteration 1: b = 170; b+b = 340; bin(b) = 10101010; bin(b+b) = 101010100
Iteration 2: b = 340; b+b = 680; bin(b) = 101010100; bin(b+b) = 1010101000
Iteration 3: b = 680; b+b = 1360; bin(b) = 1010101000; bin(b+b) = 10101010000
Iteration 4: b = 1360; b+b = 2720; bin(b) = 10101010000; bin(b+b) = 101010100000
Iteration 5: b = 2720; b+b = 5440; bin(b) = 101010100000; bin(b+b) = 1010101000000
Iteration 6: b = 5440; b+b = 10880; bin(b) = 1010101000000; bin(b+b) = 10101010000000
Iteration 7: b = 10880; b+b = 21760; bin(b) = 10101010000000; bin(b+b) = 101010100000000
Iteration 8: b = 21760; b+b = 43520; bin(b) = 101010100000000; bin(b+b) = 1010101000000000
Iteration 9: b = 43520; b+b = 87040; bin(b) = 1010101000000000; bin(b+b) = 10101010000000000
Iteration 10: b = 87040; b+b = 174080; bin(b) = 10101010000000000; bin(b+b) = 101010100000000000
Iteration 11: b = 174080; b+b = 348160; bin(b) = 101010100000000000; bin(b+b) = 1010101000000000000
Iteration 12: b = 348160; b+b = 696320; bin(b) = 1010101000000000000; bin(b+b) = 10101010000000000000
Iteration 13: b = 696320; b+b = 1392640; bin(b) = 10101010000000000000; bin(b+b) = 101010100000000000000
Iteration 14: b = 1392640; b+b = 2785280; bin(b) = 101010100000000000000; bin(b+b) = 1010101000000000000000
Iteration 15: b = 2785280; b+b = 5570560; bin(b) = 1010101000000000000000; bin(b+b) = 10101010000000000000000
Iteration 16: b = 5570560; b+b = 11141120; bin(b) = 10101010000000000000000; bin(b+b) = 101010100000000000000000
Iteration 17: b = 11141120; b+b = 22282240; bin(b) = 101010100000000000000000; bin(b+b) = 1010101000000000000000000
Iteration 18: b = 22282240; b+b = 44564480; bin(b) = 1010101000000000000000000; bin(b+b) = 10101010000000000000000000
Iteration 19: b = 44564480; b+b = 89128960; bin(b) = 10101010000000000000000000; bin(b+b) = 101010100000000000000000000
Iteration 20: b = 89128960; b+b = 178257920; bin(b) = 101010100000000000000000000; bin(b+b) = 1010101000000000000000000000
Iteration 21: b = 178257920; b+b = 356515840; bin(b) = 1010101000000000000000000000; bin(b+b) = 10101010000000000000000000000
Iteration 22: b = 356515840; b+b = 713031680; bin(b) = 10101010000000000000000000000; bin(b+b) = 101010100000000000000000000000
Iteration 23: b = 713031680; b+b = 1426063360; bin(b) = 101010100000000000000000000000; bin(b+b) = 1010101000000000000000000000000
Iteration 24: b = 1426063360; b+b = -1442840576; bin(b) = 1010101000000000000000000000000; bin(b+b) = 10101010000000000000000000000000
Iteration 25: b = -1442840576; b+b = 1409286144; bin(b) = 10101010000000000000000000000000; bin(b+b) = 1010100000000000000000000000000
Iteration 26: b = 1409286144; b+b = -1476395008; bin(b) = 1010100000000000000000000000000; bin(b+b) = 10101000000000000000000000000000
Iteration 27: b = -1476395008; b+b = 1342177280; bin(b) = 10101000000000000000000000000000; bin(b+b) = 1010000000000000000000000000000
Iteration 28: b = 1342177280; b+b = -1610612736; bin(b) = 1010000000000000000000000000000; bin(b+b) = 10100000000000000000000000000000
Iteration 29: b = -1610612736; b+b = 1073741824; bin(b) = 10100000000000000000000000000000; bin(b+b) = 1000000000000000000000000000000
Iteration 30: b = 1073741824; b+b = -2147483648; bin(b) = 1000000000000000000000000000000; bin(b+b) = 10000000000000000000000000000000
Iteration 31: b = -2147483648; b+b = 0; bin(b) = 10000000000000000000000000000000; bin(b+b) = 0

You're essentially shifting bits to the left, logically equivalent to multiplying by 2 at each iteration. You end up with binary 10000000000000000000000000000000 at iteration 31 and shifting this to the left will push the 1 out of the sequence so you're left with 00000000000000000000000000000000. This happens at iteration 31 regardless of your starting value for b (you only have 32 bits to work with, positions 0-31).

Addendum: other ways to write b += b:

b <<= 1
b *= 2
geco17
  • 5,152
  • 3
  • 21
  • 38
1

Just add a print statement inside that for loop, something like below:

public static void main(String argv[]){
    int a = 869;
    int b = 85;
    for(int i = 0; i < a; i++){
      b += b;
      System.out.println(b);          <----- Add this to see 
    }
    System.out.println(b);
  }

Output:

170
340
680
1360
2720
5440
10880
21760
43520
87040
174080
348160
696320
1392640
2785280
5570560
11141120
22282240
44564480
89128960
178257920
356515840
713031680
1426063360
-1442840576
1409286144
-1476395008
1342177280
-1610612736
1073741824
-2147483648
0
0
0
0
0
0
.....

And so on and on till the loop ends.

Note that the limit you've set has been crossed way too earlier. Now the reason why you see 0 from here onwards is because of the link as mentioned here and here

Do note in detailed the answer i.e 2nd link shared. That would help you understand why it behaves the way it behaves and I think it is not just limited to int but also in case of long, double and all other numeric types.

Hope that helps!

Kamal Kunjapur
  • 8,547
  • 2
  • 22
  • 32
  • Thanks, but @curiosa's links only talks about non-integer data types returning 0. I still don't understand why an int does that. Thanks. – Nik Vinter Oct 13 '19 at 16:58
  • You can check this answer where it has been described as why this happens https://stackoverflow.com/a/3001995/3838328 and its pretty thorough. – Kamal Kunjapur Oct 13 '19 at 17:00