What does a bitwise exclusive OR do in Java?

Java Player picture Java Player · Sep 6, 2012 · Viewed 10k times · Source

Given:

public class Spock {
    public static void main(String[] args) {
        Long tail = 2000L;
        Long distance = 1999L;
        Long story = 1000L;
        if ((tail > distance) ^ ((story * 2) == tail)) {
            System.out.print("1");
        }
        if ((distance + 1 != tail) ^ ((story * 2) == distance)) {
            System.out.print("2");
        }
    }
}

Why this sample code doesn't output anything?

Answer

Ilya picture Ilya · Sep 6, 2012

In first if you get true ^ true = false
In second if you get false ^ false = false
becouse ^ - is OR exclusive opeartor, it's means

true ^ true = false  
true ^ false = true 
false ^ true = true 
false ^ false = false