Java XOR

The Bitwise operators are typically used to manipulate or query the binary indexed tree. They are employed to carry out the operations on separate bits of numbers. We can use them on any integral data type including int, char, short, etc. We will cover one of the Bitwise operators which is the XOR operator of Java. The XOR which is also termed exclusive OR requires two Boolean operands. The true form of the XOR is returned when the operands hold different values. The XOR operator is most effective when neither of the two given Boolean conditions are true at the same time.

Example 1:

The XOR Binary operator is computed from left to right when used for the program. The XOR “^” operator is undefined for the string data type arguments.

public class XORExample1 {
public static void main(String[] args) {

boolean val1 = true;
boolean val2 = false;
boolean Result = val1 ^ val2;
System.out.println("val1 val2: "+ Result);
  val1 = true;
  val2 = true;
  Result = val1 ^ val2;
System.out.println("val1 ^ val2: "+ Result);
  val1 = false;
  val2 = false;
  Result = val1 ^ val2;
System.out.println("val1 val2: "+ Result);
  val1 = true;
  val2 = false;
  Result = val1 ^ val2;
System.out.println("val1 ^ val2: '+ Result);

We create the Boolean variables “val1” and “val2” which are given the Boolean values. The “result” variable of the Boolean type is defined which is employed with the XOR operator to evaluate the XOR on both “val1” and “val2” operands. The println() method prints the outcomes of the XOR operation. Next, we change the operand’s Boolean values and assign the false values to get different results. Then, we provide the alternate Boolean values to these operands to show the functionality of the XOR operator when one operand has a true value and the other one has a false value.

The different cases for the XOR operator generate different results. The similar Boolean values always give false values and the different values of the operand always return true for the XOR operator.

Example 2:

The XOR operator for the Boolean values of the operands is demonstrated in the first example. Now, the XOR operator can also be applied to the numerical value for Bitwise XOR operation.

public class XORExample2{
public static void main(String[] args) {

    int a = 8;
    int b = 2;

System.out.println("a ^ b = " + (a ^ b));

We declare the “a” variable of int type which is specified with the “8” integer value. The next variable which is “b” is also initialized with the “2” number value. After that, we use the println() method of Java to perform the XOR operation on the previously-specified variables by calling the XOR equation which is evaluated and printed.

The binary value of “8” is “1000”. And the binary value for “2” is “10”. The obtained result in binary form for these operands is “1010” which is the value of “10” which is displayed in the following terminal:

Example 3:

The XOR of the binary strings can be determined using our method which employs the XOR operator and some further logic. The XOR operator is used for both strings by simply looping through each character in both strings at the same time. Take into account that XOR can operate on char data types and returns 0 if the characters are similar.

public class XORExample3{
public static String XORBinaryString(String bs1, String bs2){
  String X = "";
if(bs1.length() >bs2.length()){
  String temp = "" ;
for(int i = 0; i bs1.length()){
  String temp = "";
for(int i = 0; i< bs2.length() bs1.length(); i++)
  temp += "0";
  bs1 = temp + bs1;
  for (int i=0; i< bs1.length(); i++){
  X += bs1.charAt(i) ^ bs2.charAt(i);
 return X;
public static void main(String[] args) {
System.out.println("1011 ^ 1111:" + XORBinaryString("1011 ^ 1111"));
System.out.println("1 ^ 111101:" + XORBinaryString("1 ^ 11101"));
System.out.println("0101 ^ 1:" + XORBinaryString("0101 ^ 1"));
System.out.print("10000010 ^ 0:" + XORBinaryString("1000001 ^ 0")+"\n");

We construct the “XORBinaryString” function which takes the bs1 and bs2 variables of string type as a constructor argument. In the “XORBinaryString” function, we declare the variable “X” which initially contains an empty string. After that, we have the if-else-if conditional statement to add the zero to these “bs1” and “bs2” string variables to make the length of the binary strings equal. Then, we have the for-loop deployment which traverses each character of the “bs1” and “bs2” binary strings and use these binary strings with the XOR operator to obtain the XOR results from them. The main() method is used to assign the binary values against the binary strings by calling the “XORBinaryString” function.

The XOR operator results are obtained for the binary string which holds the different binary values which are obtained using the custom method.

Example 4:

The NOT operator can be used to carry out the same task as the XOR operator in Java. Since the XOR operator only returns true if both conditions are different, using the NOT operator enables us to compare whether the first condition and the second condition are equal or not, returning true if they are not equal and false otherwise.

public class XORExample4 {
public static void main(String[] args) {

boolean x1= true;
boolean x2= false;
boolean x3= true;
boolean x4= false;
if ((x1 = x3)) {
System.out.println("Condition of XOR is true");
System.out.println("Condition of XOR is false");

We declare some Boolean variables which are titled “x1”, “x2”, “x3” and “x4”. These are initialized with the Boolean values. Some are specified with the true value, and some contain false. Then, we have an if-else condition block where we define the condition of the NOT operator if the “x1” variable is not equal to the “x2” variable. The specified condition is efficient that it only requires one true condition to be true. However, its logic requires several conditions.

The output of the alternate way for the XOR operation displays the following results:

Example 5:

The XOR operator can also be used to swap the integer values without utilizing a third variable. This approach acquires less memory and has a lower temporal complexity.

public class XORExample5 {
static void swap(int m, int n) {
System.out.println("Before Swapping.");
System.out.println("m=" + m);
System.out.println("n=" + n);
   m ^= n;
   n ^= m;
   m ^= n;
System.out.println("After Swapping.");
System.out.println("m = " + m);
System.out.println("n = " + n);
public static void main(String[] args) {
swap(21, 15);

We establish a “swap()” function inside the Java main class. The “swap()” function constructor is specified with the “m” and “n” parameters. After that, we print the “m” and “n” variables to show the values before the swapping operation. Next, we apply the swap technique on the declared “m” and “n” variables using the XOR operator. Then, print the swapped values for the “m” and “n” variables. The main() method is called the swap() function where we initialize the numerical values for the “m” and “n” variables for swapping.

The before-swapping values are printed on the console along with the results of the after swapping operation:

Example 6:

Now, there is a complex scenario of the XOR operator to search for the non-repeating values from the given array.

public class XORExample6{
public static int nonRepeatingInteger (int[] intArray){
  int MyXor
for(int i = 1; i<intArray.length; i++)
MyXor = MyXor ^ intArray[i];
  return MyXor;
public static void main(String[] args){
int[] MyArray = {19, 26, 45, 26, 45, 82, 19};
  int nonRepeat = nonRepeatingInteger (MyArray);
System.out.print("Non-repeating integers: +nonRepeat + "\n");

We create the “nonRepeatingInteger” function which calls the constructor to declare the int[] array “intArray”. Then, within the “nonRepeatingInteger”, we define the “MyXor” object and specify the “intArray” array of length zero which is initialized with the integer values in the main() method. The array is set with duplicate values except for one value. The for-loop is used to get the values from the arrays. Then, these array values are arranged as (19^ 19) ^ (26 ^ 26 ) ^ (45 ^ 45) ^ 82 in the “MyXor” variable. As we know, the XOR operator returns zero for similar values. So, all the similar values of the array become zero and the non-similar value is obtained from the XOR.

The number which is not repeated in the array is “82” which we retrieved inside the terminal after the XOR technique.


We covered the fundamentals of the XOR operation in this article. It acquires fewer memory-intensive operators and is simpler to implement. We utilized the XOR operator in the given program of Java. We are also given a technique to determine the XOR values of binary strings.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.