Search code examples
javabitwise-operators

Basic question about java bit-wise operation


Can anyone explain why the first println output 0 and the second println output 2?

int count = 1;
System.out.println(count + 1&1);
System.out.println(count += 1&1);

Solution

  • Simple order of evaluation. The first example is equivalent to

    System.out.println((count + 1) & 1);
    

    which is (of course) System.out.println(2 & 1); which is 0.