Abstract


  • Exclusive OR, difference
  • Returns true only when 2 inputs aren’t the same

Properties


Self-Inverse

  • a ^ a = 0
  • a XOR a is always 0

Own-Inverse

  • a ^ b ^ b = a
  • W always get back the another half by XOR the another half one more time. Because Self-Inverse, b ^ b = 0

Tricks


Swapping values

public class MyClass {
    public static void main(String args[]) {
      int x=10;
      int y=25;
      
      x = x ^ y; // x ^ x = y, x ^ y = x
      y = x ^ y; // x
      x = x ^ y; // y
 
      System.out.println("x: " + x);
      System.out.println("y: " + y);
    }
}