- Logical xor in java
- Java XOR (^) Operator Explained [Practical Examples]
- What is XOR (^) Operator
- How to use the XOR operator in Java
- How to use Java XOR (^) as Logical Operator
- Example — How to get XOR using logical AND(&&) and logical OR(||)
- Example — How to get XOR using logical NOT(!) operator
- Example — Java XOR (^) Operator
- How to use Java XOR (^) as Bitwise Operator
- Example — Swap numbers using Java XOR (^) Operator
- Practice code which uses XOR for different functionalities
- Conclusion
- Further reading
Logical xor in java
- Binary representation of a given number
- Count set bits in an integer
- Add two bit strings
- Turn off the rightmost set bit
- Rotate bits of a number
- Compute modulus division by a power-of-2-number
- Find the Number Occurring Odd Number of Times
- Program to find whether a given number is power of 2
- Find position of the only set bit
- Check for Integer Overflow
- Find XOR of two number without using XOR operator
- Check if two numbers are equal without using arithmetic and comparison operators
- Detect if two integers have opposite signs
- How to swap two numbers without using a temporary variable?
- Russian Peasant (Multiply two numbers using bitwise operators)
- Swap bits in a given number
- Smallest of three integers without comparison operators
- Compute the minimum or maximum of two integers without branching
- Smallest power of 2 greater than or equal to n
- Program to find parity
- Check if binary representation of a number is palindrome
- Generate n-bit Gray Codes
- Check if a given number is sparse or not
- Euclid’s Algorithm when % and / operations are costly
- Calculate square of a number without using *, / and pow()
- Copy set bits in a range
- Check if a number is Bleak
- Gray to Binary and Binary to Gray conversion
- Next higher number with same number of set bits
- Find the maximum subarray XOR in a given array
- Find longest sequence of 1’s in binary representation with one flip
- Closest (or Next) smaller and greater numbers with same number of set bits
- Bitmasking and Dynamic Programming | Travelling Salesman Problem
- Compute the parity of a number using XOR and table look-up
- XOR Encryption by Shifting Plaintext
- Count pairs in an array which have at least one digit common
- Python program to convert floating to binary
- Booth’s Multiplication Algorithm
- Number of pairs with Pandigital Concatenation
- Find the n-th number whose binary representation is a palindrome
- Find the two non-repeating elements in an array of repeating elements/ Unique Numbers 2
- Write an Interview Experience
- Share Your Campus Experience
- Bitwise Algorithms
- Introduction to Bitwise Algorithms – Data Structures and Algorithms Tutorial
- Bitwise Operators in C/C++
- Bitwise Operators in Java
- Python Bitwise Operators
- JavaScript Bitwise Operators
- All about Bit Manipulation
- Little and Big Endian Mystery
- Bits manipulation (Important tactics)
- Binary representation of a given number
- Count set bits in an integer
- Add two bit strings
- Turn off the rightmost set bit
- Rotate bits of a number
- Compute modulus division by a power-of-2-number
- Find the Number Occurring Odd Number of Times
- Program to find whether a given number is power of 2
- Find position of the only set bit
- Check for Integer Overflow
- Find XOR of two number without using XOR operator
- Check if two numbers are equal without using arithmetic and comparison operators
- Detect if two integers have opposite signs
- How to swap two numbers without using a temporary variable?
- Russian Peasant (Multiply two numbers using bitwise operators)
- Swap bits in a given number
- Smallest of three integers without comparison operators
- Compute the minimum or maximum of two integers without branching
- Smallest power of 2 greater than or equal to n
- Program to find parity
- Check if binary representation of a number is palindrome
- Generate n-bit Gray Codes
- Check if a given number is sparse or not
- Euclid’s Algorithm when % and / operations are costly
- Calculate square of a number without using *, / and pow()
- Copy set bits in a range
- Check if a number is Bleak
- Gray to Binary and Binary to Gray conversion
- Next higher number with same number of set bits
- Find the maximum subarray XOR in a given array
- Find longest sequence of 1’s in binary representation with one flip
- Closest (or Next) smaller and greater numbers with same number of set bits
- Bitmasking and Dynamic Programming | Travelling Salesman Problem
- Compute the parity of a number using XOR and table look-up
- XOR Encryption by Shifting Plaintext
- Count pairs in an array which have at least one digit common
- Python program to convert floating to binary
- Booth’s Multiplication Algorithm
- Number of pairs with Pandigital Concatenation
- Find the n-th number whose binary representation is a palindrome
- Find the two non-repeating elements in an array of repeating elements/ Unique Numbers 2
Java XOR (^) Operator Explained [Practical Examples]
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.
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:
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:
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"); >> >
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
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
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 > > 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.