Java - Bitwise XOR Operator ^

What is Bitwise XOR Operator?

Bitwise XOR ^ operates returns 1 if only one of the bits is 1. Otherwise, it returns 0.

Consider the following code,

int i = 13 ^ 3;

Java int uses 32 bits in 8-bit chunks in memory.

13 is 00000000 00000000 00000000 00001101

3 is 00000000 00000000 00000000 00000011

13 ^ 3

becomes

00000000 00000000 00000000 00001101
00000000 00000000 00000000 00000011
-----------------------------------
00000000 00000000 00000000 00001110

The result of 13 ^ 3 is 14.

Demo

public class Main {
  public static void main(String[] args) {
    int i = 13 ^ 3;
    System.out.println("i = " + i);
  }//from  w w  w.j  av  a  2  s  . c om
}

Result