96

I have a simple program:

public class Mathz {
    static int i = 1;
    public static void main(String[] args) {    
        while (true){
            i = i + i;
            System.out.println(i);
        }
    }
}

When I run this program, all I see is 0 for i in my output. I would have expected the first time round we would have i = 1 + 1, followed by i = 2 + 2, followed by i = 4 + 4 etc.

Is this due to the fact that as soon as we try to re-declare i on the left hand-side, its value gets reset to 0?

If anyone can point me into the finer details of this that would be great.

Change the int to long and it seems to be printing numbers as expected. I'm surprised at how fast it hits the max 32-bit value!

0

10 Answers 10

334

Introduction

The problem is integer overflow. If it overflows, it goes back to the minimum value and continues from there. If it underflows, it goes back to the maximum value and continues from there. The image below is of an Odometer. I use this to explain overflows. It's a mechanical overflow but a good example still.

In an Odometer, the max digit = 9, so going beyond the maximum means 9 + 1, which carries over and gives a 0 ; However there is no higher digit to change to a 1, so the counter resets to zero. You get the idea - "integer overflows" come to mind now.

enter image description here enter image description here

The largest decimal literal of type int is 2147483647 (231-1). All decimal literals from 0 to 2147483647 may appear anywhere an int literal may appear, but the literal 2147483648 may appear only as the operand of the unary negation operator -.

If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format. If overflow occurs, then the sign of the result is not the same as the sign of the mathematical sum of the two operand values.

Thus, 2147483647 + 1 overflows and wraps around to -2147483648. Hence int i=2147483647 + 1 would be overflowed, which isn't equal to 2147483648. Also, you say "it always prints 0". It does not, because http://ideone.com/WHrQIW. Below, these 8 numbers show the point at which it pivots and overflows. It then starts to print 0s. Also, don't be surprised how fast it calculates, the machines of today are rapid.

268435456
536870912
1073741824
-2147483648
0
0
0
0

Why integer overflow "wraps around"

Original PDF

12
  • 17
    I've added the animation for "Pacman" for symbolic purposes but it also serves as a great visual of how one would see 'integer overflows'.
    – Ali Gajani
    Jun 12, 2014 at 11:44
  • 9
    This is my favorite answer on this site ever of all times.
    – Lee White
    Jun 12, 2014 at 14:24
  • 2
    You seem to have missed that this was a doubling sequence, not adding one. Jun 12, 2014 at 19:25
  • 2
    I think the pacman animation got this answer more upvotes than the accepted answer. Have another upvote on me - its one of my favourite games!
    – Husman
    Jun 13, 2014 at 10:35
  • 3
    For anyone who didn't get the symbolism: en.wikipedia.org/wiki/Kill_screen#Pac-Man
    – wei2912
    Jun 14, 2014 at 13:19
168

The issue is due to integer overflow.

In 32-bit twos-complement arithmetic:

i does indeed start out having power-of-two values, but then overflow behaviors start once you get to 230:

230 + 230 = -231

-231 + -231 = 0

...in int arithmetic, since it's essentially arithmetic mod 2^32.

10
  • 28
    Could you expand on your answer a bit?
    – DeaIss
    Jun 11, 2014 at 22:16
  • 17
    @oOTesterOo It starts of printing 2, 4 etc but it very quickly reaches the maximum value of the integer and it "wraps around" to negative numbers, once it reaches zero it stays at zero forever Jun 11, 2014 at 22:17
  • 52
    This answer isn't even complete (it doesn't even mention that the value will not be 0 on the first few iterations, but speed of output is obscuring that fact from the OP). Why is it accepted? Jun 12, 2014 at 10:09
  • 16
    Presumably it was accepted because it was considered helpful by the OP.
    – Joe
    Jun 12, 2014 at 15:33
  • 4
    @LightnessRacesinOrbit Although it doesn't directly address the issues the OP put in their question, the answer gives enough information that a decent programmer should be able to infer what's going on.
    – Kevin
    Jun 13, 2014 at 17:34
46

No, it does not print only zeros.

Change it to this and you will see what happens.

    int k = 50;
    while (true){
        i = i + i;
        System.out.println(i);
        k--;
        if (k<0) break;
    }

What happens is called overflow.

5
  • 61
    Interesting way to write a for loop :)
    – Bernhard
    Jun 12, 2014 at 5:59
  • 17
    @Bernhard It's probably to keep the structure of OP's program.
    – Taemyr
    Jun 12, 2014 at 9:41
  • 4
    @Taemyr Probably, but then he could have replaced true with i<10000 :)
    – Bernhard
    Jun 12, 2014 at 10:12
  • 7
    I just wanted to add a few statements; without deleting/changing any statements. I'm surprised it attracted such a wide attention. Jun 12, 2014 at 13:29
  • 18
    You could have used the hidden operator while(k --> 0) colloquially named "while k goes to 0" ;) Jun 13, 2014 at 13:37
15
static int i = 1;
    public static void main(String[] args) throws InterruptedException {
        while (true){
            i = i + i;
            System.out.println(i);
            Thread.sleep(100);
        }
    }

out put:

2
4
8
16
32
64
...
1073741824
-2147483648
0
0

when sum > Integer.MAX_INT then assign i = 0;
1
  • 4
    Um, no, it just works for this particular sequence to get to zero. Try starting with 3. Jun 12, 2014 at 19:27
4

Since I don't have enough reputation I cannot post the picture of the output for the same program in C with controlled output, u can try yourself and see that it actually prints 32 times and then as explained due to overflow i=1073741824 + 1073741824 changes to -2147483648 and one more further addition is out of range of int and turns to Zero .

#include<stdio.h>
#include<conio.h>

int main()
{
static int i = 1;

    while (true){
        i = i + i;
      printf("\n%d",i);
      _getch();
    }
      return 0;
}
9
  • 3
    This program, in C, actually triggers undefined behavior in every execution, which allows the compiler to replace the whole program with anything (even system("deltree C:"), since you're in DOS/Windows). Signed integer overflow is undefined behavior in C/C++, unlike Java. Be very careful when using this kind of construct.
    – filcab
    Jun 12, 2014 at 17:13
  • @filcab : "replace the whole program with anything" what are you talking about. I have run this program on Visual studio 2012 and it runs perfectly fine for both signed and unsigned integers without any undefined behavior
    – Kaify
    Jun 13, 2014 at 0:03
  • 3
    @Kaify: Working fine is a perfectly valid undefined behaviour. Imagine however that the code did i += i for 32+ iterations, then had if (i > 0). The compiler could optimise that to if(true) since if we're always adding positive numbers, i will always be greater than 0. It could also leave the condition in, where it won't be executed, because of the overflow represented here. Because the compiler could produce two equally valid programs from that code, it's undefined behaviour.
    – 3Doubloons
    Jun 13, 2014 at 0:13
  • 1
    @Kaify: it's not lexical analysis, it's the compiler compiling your code and, following the standard, being able to do “weird” optimizations. Like the loop that 3Doubloons talked about. Just because compilers that you try always seem to do something, it doesn't mean the standard guarantees your program will always run the same way. You had undefined behavior, some code might have been eliminated since there's no way to get there (UB guarantees that). These posts from the llvm blog (and links in there) have more information: blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
    – filcab
    Jun 13, 2014 at 3:09
  • 2
    @Kaify: Sorry for not putting it, but it's completely wrong to say “kept it a secret”, especially when it's the second result, on Google, for “undefined behavior”, which was the specific term I used for what was being triggered.
    – filcab
    Jun 15, 2014 at 3:14
4

The value of i is stored in memory using a fixed quantity of binary digits. When a number needs more digits than are available, only the lowest digits are stored (the highest digits get lost).

Adding i to itself is the same as multiplying i by two. Just like multiplying a number by ten in decimal notation can be performed by sliding each digit to the left and putting a zero on the right, multiplying a number by two in binary notation can be performed the same way. This adds one digit on the right, so a digit gets lost on the left.

Here the starting value is 1, so if we use 8 digits to store i (for example),

  • after 0 iterations, the value is 00000001
  • after 1 iteration , the value is 00000010
  • after 2 iterations, the value is 00000100

and so on, until the final non-zero step

  • after 7 iterations, the value is 10000000
  • after 8 iterations, the value is 00000000

No matter how many binary digits are allocated to store the number, and no matter what the starting value is, eventually all of the digits will be lost as they are pushed off to the left. After that point, continuing to double the number will not change the number - it will still be represented by all zeroes.

3

It is correct, but after 31 iterations, 1073741824 + 1073741824 doesn't calculate correctly (overflows) and after that prints only 0.

You can refactor to use BigInteger, so your infinite loop will work correctly.

public class Mathz {
    static BigInteger i = new BigInteger("1");
    
    public static void main(String[] args) {    
        
        while (true){
            i = i.add(i);
            System.out.println(i);
        }
    }
}
8
  • If I use long instead of int, it seems to be print >0 numbers for a long time. Why does it not run into this problem after 63 iterations?
    – DeaIss
    Jun 11, 2014 at 22:20
  • 1
    "Doesn't calculate correctly" is an incorrect characterization. The calculation is correct according to what the Java spec says should happen. The real issue is that the result of the (ideal) calculation cannot be represented as an int.
    – Stephen C
    Jun 11, 2014 at 22:22
  • @oOTesterOo - because long can represent larger numbers than int can.
    – Stephen C
    Jun 11, 2014 at 22:23
  • Long has a larger range. The BigInteger type accepts any value/length that your JVM can allocate. Jun 11, 2014 at 22:23
  • I assumed int overflows after 31 iterations because its a 32-bit max sized number, and so long which is a 64-bit would hit its max after 63? Why is that not the case?
    – DeaIss
    Jun 11, 2014 at 22:24
2

For debugging such cases it is good to reduce the number of iterations in the loop. Use this instead of your while(true):

for(int r = 0; r<100; r++)

You can then see that it starts with 2 and is doubling the value until it is causing an overflow.

2

I'll use an 8-bit number for illustration because it can be completely detailed in a short space. Hex numbers begin with 0x, while binary numbers begin with 0b.

The max value for an 8-bit unsigned integer is 255 (0xFF or 0b11111111). If you add 1, you would typically expect to get: 256 (0x100 or 0b100000000). But since that's too many bits (9), that's over the max, so the first part just gets dropped, leaving you with 0 effectively (0x(1)00 or 0b(1)00000000, but with the 1 dropped).

So when your program runs, you get:

1 = 0x01 = 0b1
2 = 0x02 = 0b10
4 = 0x04 = 0b100
8 = 0x08 = 0b1000
16 = 0x10 = 0b10000
32 = 0x20 = 0b100000
64 = 0x40 = 0b1000000
128 = 0x80 = 0b10000000
256 = 0x00 = 0b00000000 (wraps to 0)
0 + 0 = 0 = 0x00 = 0b00000000
0 + 0 = 0 = 0x00 = 0b00000000
0 + 0 = 0 = 0x00 = 0b00000000
...
1

The largest decimal literal of type int is 2147483648 (=231). All decimal literals from 0 to 2147483647 may appear anywhere an int literal may appear, but the literal 2147483648 may appear only as the operand of the unary negation operator -.

If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format. If overflow occurs, then the sign of the result is not the same as the sign of the mathematical sum of the two operand values.

Not the answer you're looking for? Browse other questions tagged or ask your own question.