Xor operator in java – Java Program on Bitwise XOR Operator

Xor operator in java: In the previous article we have discussed about Java Program on Bitwise OR Operator

In this article we are going to see the use Bitwise XOR operator in Java.

Java Program on Bitwise XOR Operator

Bitwise XOR Operator:

Java bitwise xor: Bitwise XOR operator is a binary operator which is represented by ^ symbol. It performs manipulation on each bit of a number i.e. bit by bit XOR operation. It means it returns 1 if both the bits are different else it returns 0.

Bitwise XOR Operation


operand1 ^ operand2


  • operand1 refers to first operand
  • ^ refers to Bitwise XOR operator
  • operand2 refers to second operand


A = 2 in binary 0010
B = 3 in binary 0011
A ^ B = 3 = 0001

Let’s see an program to understand it more clearly.


  • Declare two number.
  • Perform Bitwise XOR operation on them by using operator.
  • Print the result.
public class Main
    public static void main(String[] args)   
        //declaring two numbers
        int a = 2, b = 3;   
        //performing Bitwise XOR operation
        int result = a ^ b;
        //printing result
        System.out.println("a ^ b = "+result);   

a ^ b = 1

topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: