Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

XOR Operator in Java

XOR is a bitwise operator and, it works by comparing individual bits of the operands. XOR stands for eXclusive OR and, it returns true if and only if the two bits being compared are not the same. The following truth table explains the output of the XOR operation. Remember that in binary 1 stands for true and 0 stands for false.

A B A XOR B
0 0 0
0 1 1
1 0 1
1 1 0

XOR can also be written in terms of other basic operators like AND, OR, and NOT. A XOR B is equivalent to (A AND (NOT B)) OR (B AND (NOT A)). We can write XOR in this way because at any instant either A should be True and B should be False, or A should be False and B should be True.

Example: XOR in Java

In Java, the XOR operator is represented by the caret symbol(^). It can be used on any primitive data type. Let's try to verify the truth table of XOR using this operator.

public static void main(String[] args)
{
	System.out.println("0 XOR 0: " + (0 ^ 0));
	System.out.println("0 XOR 1: " + (0 ^ 1));
	System.out.println("1 XOR 0: " + (1 ^ 0));
	System.out.print("1 XOR 1: " + (1 ^ 1));
}


0 XOR 0: 0
0 XOR 1: 1
1 XOR 0: 1
1 XOR 1: 0

Example: XOR with Boolean Values

XOR can also work on boolean values. Let's try to recreate the table using boolean true and false.

public static void main(String[] args)
{
	System.out.println("False XOR False: " + (false ^ false));
	System.out.println("False XOR True: " + (false ^ true));
	System.out.println("True XOR False: " + (true ^ false));
	System.out.print("True XOR True: " + (true ^ true));
}


False XOR False: false
False XOR True: true
True XOR False: true
True XOR True: false

Example: XOR with Integer Values in Java

XOR operator can be used on integers that are not 0 or 1. The XOR operator will work on the individual bits of the binary equivalent of the integer. For example, if 9 is XORed with 15, then the XOR operator will be applied on the individual bits of the two numbers(1001 for 9 and 1111 for 15).

XOR operation between integers

public static void main(String[] args)
{
	System.out.println("9 XOR 15: " + (9 ^ 15));//1001 XOR 1111 = 0110
	System.out.println("1 XOR 20: " + (1 ^ 20));//00001 XOR 10100 = 10101
	System.out.println("7 XOR 7: " + (7 ^ 7));//0111 XOR 0111 = 0000
	System.out.print("32 XOR 0: " + (32 ^ 0));//100000 XOR 000000 = 100000
}


9 XOR 15: 6
1 XOR 20: 21
7 XOR 7: 0
32 XOR 0: 32

XOR of Binary Strings

XOR only works for primitive data types. However, we can write our own method that uses the XOR operator and some additional logic to find the XOR of two binary strings. We will simply loop through each character of both strings simultaneously and use the XOR operator on them. Remember that XOR can work on char data type and returns 0 if both characters are the same.

public class XOR
{
	public static String binaryStringXOR(String binStr1, String binStr2)
	{
		String xor = "";		
		//adding zeroes to make the two strings equal in length
		if(binStr1.length() > binStr2.length())
		{
			String temp = "";
			for(int i = 0; i < binStr1.length() - binStr2.length(); i++)
				temp += "0";
			binStr2 = temp + binStr2;
		}
		else if(binStr2.length() > binStr1.length())
		{
			String temp = "";
			for(int i = 0; i < binStr2.length() - binStr1.length(); i++)
				temp += "0";
			binStr1 = temp + binStr1;
		}		
		for(int i=0; i < binStr1.length(); i++)
		{
			xor += binStr1.charAt(i) ^ binStr2.charAt(i);
		}		
		return xor;
	}
	public static void main(String[] args)
	{
		System.out.println("1001 XOR 1111: " + binaryStringXOR("1001", "1111"));
		System.out.println("1 XOR 10100: " + binaryStringXOR("1", "10100"));
		System.out.println("0111 XOR 1: " + binaryStringXOR("0111", "1"));
		System.out.print("100000 XOR 0: " + binaryStringXOR("100000", "0"));
	}
}


1001 XOR 1111: 0110
1 XOR 10100: 10101
0111 XOR 1: 0110
100000 XOR 0: 100000

Example: Find non repeating value using xor in Java

We learned the basics of the XOR operator. Now let's use it to solve a problem. Suppose we have an array of integers and we know that each integer occurs exactly twice except one particular integer. Our task is to find this non-repeating integer.

We can simply iterate over the array, and store the frequency of each integer and return the integer that occurs only once. But there is a more efficient way of doing this by using the XOR operator. Remember that XOR of a number with itself returns 0, and XOR of a number with 0 returns the number itself.

For example, consider the input array [10, 12, 5, 6, 10, 6, 12]. We can see that all elements occur twice except 5. The XOR of all elements can be written as 10 ^ 12 ^ 5 ^ 6 ^ 10 ^ 6 ^ 12. This can be rearranged and written as (10 ^ 10) ^ (12 ^ 12 ) ^ (6 ^ 6) ^ 5. This is the same as writing 0 ^ 0 ^ 0 ^ 5, and this will return 5.

public class XOR
{
	public static int nonRepeatingInteger(int[] numArray)
	{
		int xor = numArray[0];
		for(int i = 1; i < numArray.length; i++)
			xor = xor ^ numArray[i];
		return xor;
	}
	public static void main(String[] args)
	{
		int[] arr = {10, 12, 5, 6, 10, 6, 12};
		int nonRepeatingNum = nonRepeatingInteger(arr);
		System.out.print("The non-repeating integer is:" + nonRepeatingNum);
	}
}


The non-repeating integer is:5

Summary

In this tutorial, we learned about the basics of the XOR operation. The caret symbol(^) is used as the XOR operator in Java and it can work on all primitive data types. We also implemented a method to find the XOR of two binary strings.