Java XOR (^) Operator Explained [Practical Examples]

Introduction

In this article we will discuss the Java xor (^) operator, this logical operator is used in many conditional statements, it is also used as a bitwise operator to perform tasks with efficient logic we will see how.

a = b ^ a;
b = b ^ a;
int c = a ^ b

 

What is XOR (^) Operator

The XOR operator is a logical operator which returns true only if one of the conditions is true otherwise it returns false. An output table will be as follows:

Advertisement

 

How to use the XOR operator in Java

In java, the symbol ^ is used as a XOR operator, in java, we can use the XOR operator as a logical operator and as a bitwise operator.

 

How to use Java XOR (^) as Logical Operator

Example - How to get XOR using logical AND(&&) and logical OR(||)

Consider a situation in which only one condition has to be true and all the others have to be false, if we make a logic using logical AND and logical OR but that would be a very lengthy and inefficient way. Where many logical operators are placed, the code becomes inefficient, the program has to check many conditions before getting an answer, thus an increase in time complexity.

In the code below, AND and OR operators are used to getting XOR of two conditionals, if one of the conditions is true in the code below XOR will be achieved.

package java_xor;
public class javaXor {
    public static void main(String[] args) {
        //declaring boolean variables
        boolean A = true;
        boolean B = true;
        boolean C = false;
        boolean D = false;
        // a statement that returns true if only one is true but it 
          takes a lot of conditions and this logic is inefficient
        if ((( A || C ) && ! ( A && C ))) {
            System.out.println("XOR condition is true");
        }
        else
        {
               System.out.println("XOR condition is false");
        }
    }
}

The output of the following code is:

XOR condition is true

 

Example - How to get XOR using logical NOT(!) operator

The java XOR operator function can also be achieved by using not operator, since xor operator returns true only if both the conditions are different, by using not operator we can compare, if the condition 1 is not equal to the condition 2, it returns true, otherwise false, which is exactly how the java xor operator works.

package java_xor;
public class javaXor {
    public static void main(String[] args) {
        //declaring boolean variables
        boolean A = true;
        boolean B = true;
        boolean C = false;
        boolean D = false;
        // a statement that returns true if only one is true but it takes a lot of conditions and this logic is inefficient
        if ((A != C)) {
        System.out.println("XOR condition becomes true");
        } else
        {
            System.out.println("XOR condition becomes false");
        }
    }
}

the above code output is

Advertisement
XOR condition becomes true

 

Example - Java XOR (^) Operator

In java the xor of two conditionals can be achieved using java XOR operator the ^ operator. The code below shows how java XOR operator works

package java_xor;
public class javaXor {
    public static void main(String[] args) {

        // declaring boolean variables
        boolean A = true;
        boolean B = true;
        boolean C = false;
        boolean D = false;

        // java XOR operator returns true if A and C are two 
         different conditionals
        if ((( A ^C ))) {
            System.out.println("XOR condition is true");
        }
        else
        {
          System.out.println("XOR condition is false");
        }
    }
}

The output of the following code is

XOR condition is true

 

The java XOR operator is the most efficient way to achieve the XOR condition

 

How to use Java XOR (^) as Bitwise Operator

The bitwise java XOR operator can be used to find the XOR of two decimal numbers, it checks each bit and return 1 for each bit at which both numbers bits are different and returns 0 for which both numbers bits are same, for example if we have two numbers 6 and 8 its xor would be checked by first converting it into binary and then checking each bit

0110 6
1000  8
_____
1110   14

Now lets check if the java XOR operator returns the same value or not

package java_xor;
public class javaXor {
       // function take returns XOR of 2 numbers
       public static int javaXOR(int a, int b)
       {
              return a^b;
       }
    public static void main(String[] args) {

        // declaring integers
        int a=6;
        int b=8;
        System.out.println("the XOR of these two numbers will 
          be "+javaXOR(a,b));
    }
}

 

The output of the following code will be

Advertisement
the XOR of these two numbers will be 14

 

The java XOR operators are fast, most importantly it lessens the time complexity for many algorithms like sorting arrays, swapping without taking the memory of a third variable

 

Example - Swap numbers using Java XOR (^) Operator

In the code below, the java xor operator is used to swap two integers without using a third variable, which has less time complexity and the memory used is smaller.

package java_xor;
public class javaXor {
    public static void main(String[] args) {
        // binary of 7=0111
        int a = 7;

        // binary of 8=1000
        int b = 8;
        System.out.println("Before Swapping a: " + a + " b: " + b);

        // swapping using java XOR operator
        // now a is 1111 15 and b is 8
        a = a ^ b;

        //now a is 1111 15 but b is 7 (original value of a)
        b = a ^ b;

        // now a is 8 and b is 7, numbers are swapped
        a = a ^ b;
        System.out.println("After swapping using XOR bitwise operation  a: " + a + " b: " + b);
    }
}

Output of the following code is

Before Swapping a: 7 b: 8

After swapping using XOR bitwise operation  a: 8 b: 7

 

Practice code which uses XOR for different functionalities

In the following java code, the java XOR operator has been used for multiple techniques, it is used in arrays, swapping, and other functionalities, you should dry run the following code to understand the java XOR operator well and once you determine the output of the code through dry runs, run the code in your local editor eclipse, Intellij, or Netbeans and check if the output is correct or not.

package java_xor;


public class javaXor { // show what the following function will return.     
    public static int functionFun(int a, int b) {        
     a = b ^ a;        
     b = b ^ b;        
    int c = a ^ b;        
    return c;   
  }
    // this code is similar to swap but not exactly similar, determine the output
    public static int functionSw(int a, int b) {
        a = a ^ a;
        b = b ^ b;
        int c = a ^ b;
        return c;
    }
    // what will the following function output be
    public static void arrayXOR(int[] a, int[] b) {
        //loop it till array size
        for (int i = 0; i < a.length; i++) {
            //nested loop
            for (int j = 0; i < b.length; j++) {
                //use of java xor
                a[i] = a[i] ^ a[j];
            }
        }
        for (int i = 0; i < a.length(); i++) {
            for (int j = 0; i < b.length(); j++) {
                //show output
                cout << a[i];
            }
        }
    }
    public static void main(String[] args) { 
        // declare variables.
        int a = 12;
        int b = 21;
        int c = 32;

        // show output for the following bitwise operators.        
        System.out.println("show output : " + (a ^ b ^ c ^ b ^ a));

        // used logical java XOR operator what will be the output        
        if (((a ^ b) == 23) || ((a ^ b) == 44)) {
            System.out.println("this statement is true");
        } else {
            System.out.println("this statement is false");
        }
        System.out.println("function output will be " + functionFun(a, b));
    }
}

 

Conclusion

In this article we studied how we can use java xor operators to form efficient logical conditions using java xor operator and how we can use bitwise xor to compute different mathematical equations, we also studied how we can use java xor operators to make better logic for problems like swapping two numbers without third variable. the XOR operator is used by many software engineers in complex logic formations because they are faster and less memory taking operators.

 

Further reading

To further understand the concept of java XOR and java XOR descriptor go through the links provided.

Advertisement

java xor descriptor
java operators

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X