Shift operator in Java bizarre program output

Posted by Umesh Kacha on Stack Overflow See other posts from Stack Overflow or by Umesh Kacha
Published on 2011-03-05T07:07:10Z Indexed on 2011/03/05 7:24 UTC
Read the original article Hit count: 230

Filed under:

I came across the following program and it behaving in unexpected manner.

public class ShiftProgram
{
      public static void main(String[] args)
      {
             int i = 0;
             while(-1 << i != 0)
                   i++;
             System.out.println(i);
      }
}

If we think about this program output, when it reaches 32 while loop condition should return false and terminate and it should print 32.

If you ran this program, it does not print anything but goes into an infinite loop. Any idea whats going on? Thank you in advance.

© Stack Overflow or respective owner

Related posts about java