The “XOR(exclusive OR)” refers to a vital bitwise operator in Java that enables the programmers to manipulate bits efficiently such that the calculations are optimized and the complex logical problems are solved instantly. The most efficient use case of this operator can be when both the boolean conditions cannot be “true” at the same time.
Contents Overview
- What is the XOR Operator in Java?
- Bitwise Operators in Java
- How to Use the XOR Operator in Java?
- XOR Operator Truth Table
- Working of XOR in Java
- What are the Best Practices For Using “XOR” in Java?
- Important Considerations Regarding the Java “XOR” Operator
- What is the Difference Between “XOR”, “Logical OR”, and “AND”?
What is the XOR Operator in Java?
“XOR” is a bitwise operator that takes two boolean operands as input and returns “true” or “1” if they are different whereas on the same operands, it returns “false” or “0”. This operator is evaluated from left to right and is represented by the “^” symbol.
Bitwise Operators in Java
There are 7 types of bitwise operators in Java that are used to apply bitwise operations, specified below:
Operator | Name |
---|---|
^ | Bitwise XOR |
& | Bitwise AND |
| | Bitwise OR |
~ | Bitwise Complement |
<< | Left Shift |
>> | Right Shift |
>>> | Unsigned Right Shift |
How to Use the XOR Operator in Java?
The XOR operator in Java can be used with the boolean, integer, and string values differently. Below is the working of this operator with each of these data types:
Working of XOR With Booleans
This operator returns “true” if both the values are different. Otherwise, it retrieves “false” on the same inputs.
Working of XOR With Integers
The XOR operator in this case first converts the provided integers to binary and then XOR the values such that on the different values, “1” is returned and “0” on the same inputs.
Working of XOR With Strings
In the scenario of numeric strings, the binary values are checked according to the XOR operator behavior and return “1” on different values and “0” on the same values.
XOR Operator Truth Table
a | b | a ^ b |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Working of XOR in Java
Below is the graphical illustration that XORs the binary representation of the values “2” and “3” such that the value “1” is retrieved if both the inputs are different. Otherwise the value “0” is returned:
What are the Best Practices For Using “XOR” in Java?
While using XOR in Java, ensure code readability. It is such that avoid overusing XOR for conditional logic and also consider the fact that XOR is non-associative in nature(operands order matter) while handling multiple operands. Moreover, understanding the XOR truth table is a prerequisite for resolving XOR-related limitations in the code.
Important Considerations Regarding the Java “XOR” Operator
- XOR is non-associative which implies that the operand’s order affects the outcome when dealing with multiple operands.
- XOR operates on integers and boolean values in Java and is not compatible with the floating point numbers directly.
Example 1: Applying the “XOR” Operator to Calculate the Boolean Values
This example uses the “XOR” operator to compute the different cases of the defined boolean values:
package jbArticles; public class XOR { public static void main(String[] args) { boolean a = true; boolean b = true; boolean xor = a ^ b; System.out.println(“true XOR true -> “+xor); a = false; b = true; xor = a ^ b; System.out.println(“false XOR true -> “+xor); a = true; b = false; xor = a ^ b; System.out.println(“true XOR false -> “+xor); a = false; b = false; xor = a ^ b; System.out.println(“false XOR false -> “+xor); }} |
Code Explanation
Initialize both the boolean values as “true”, XOR the inputs, and display the calculated outcome. After that, change the values of the input booleans in all the possible combination scenarios and analyze the outputs.
Output
In this output, it can be analyzed that upon the same boolean inputs, the boolean value “false” is retrieved whereas upon different inputs, the value “true” is returned.
Before moving to the next example, consider the following methods used in the example:
charAt(): This method fetches the character at the target index in a string.
Syntax
public char charAt(int ind) |
Here, “ind” points to the index of the character to be retrieved.
append(): It appends the string representation of the boolean argument.
Syntax
public StringBuilder append(boolean x) |
In this syntax, “x” refers to the boolean value that needs to be appended.
Example 2: Applying the “XOR” Operator to Calculate the String Values
In this demonstration, the “XOR” operator computes the binary string values with the help of a “StringBuffer” instance:
package jbArticles; public class XOR { public static void main(String[] args) { String val1 = “1100101001”; String val2 = “0110110110”; System.out.println(“Value 1 -> “+val1); System.out.println(“Value 2 -> “+val2); StringBuffer ob = new StringBuffer(); for (int i = 0; i < val1.length(); i++) { ob.append(val1.charAt(i) ^ val2.charAt(i)); } System.out.println(“XOR result: “+ob); } } |
Code Explanation
- Initialize the two strings as binary values and display them.
- Now, create a StringBuffer instance using the “new” keyword followed by the “StringBuffer()” constructor.
- After that, apply the “for” loop combined with the “charAt()” and “append()” methods to “XOR” both the binary strings based on the fetched length of the strings and lastly append both the values such that a single value is returned as an XOR outcome.
Output
Here, it can be verified that the XOR outcome against both binary strings is retrieved appropriately.
Example 3: Applying the “XOR” Operator to Calculate the Integer Values
This particular example utilizes the discussed operator to compute the integer values by first converting these values to binary and then computing their XOR:
package jbArticles; public class XOR { public static void main(String[] args) { System.out.println(“2 XOR 3 -> ” + (2 ^ 3)); System.out.println(“7 XOR 9 -> ” + (7 ^ 9)); System.out.println(“16 XOR 18 -> ” + (16 ^ 18)); } } |
Algorithm
2 XOR 3 = 0010 XOR 0011 = 0001Output: 2 ^ 0 = 1 7 XOR 9 = 0111 XOR 1001 = 1110 Output: 2^3 + 2^2 + 2^1 = 14 16 XOR 18 = 10000 XOR 10010 = 00010Output: 2^1 = 2 |
Code Explanation
Apply the “XOR” operator to XOR the given integer values based on the above-discussed algorithm.
Note: In the last calculation, 5 bits are used to represent “16” and “18” in binary.
Output
Example 4: Applying the “XOR” Operator to Solve a Real-Life Scenario of Finding a Non-repeating Value
In this specific example, a non-repeating value from the array will be fetched based on the XOR computation:
package jbArticles; public class XOR { public static void main(String[] args){ int[] array = {1, 2, 3, 3, 7, 2, 1}; int calXor = array[0]; for(int i = 1; i < array.length; i++) calXor = calXor ^ array[i]; System.out.print(“Non-repeating Integer in Array -> ” + calXor); } } |
Algorithm
- Input array -> {1, 2, 3, 3, 7, 2, 1};
- XOR of all array values -> 1 ^ 2 ^ 3 ^ 3 ^ 7 ^ 2 ^ 1
- The above XOR can be written as (1 ^ 1) ^ (2 ^ 2) ^ (3 ^ 3) ^ 7
- This calculation is the same as writing 0 ^ 0 ^ 0 ^ 7 since the input values of the operands are the same.
- As a result, the non-repeating integer “7” will be retrieved.
Code Explanation
- Initialize an integer array comprising the repeated integers as well as a non-repeating integer.
- In the next step, point to the first array element via indexing.
- Now, apply the “for” loop to traverse through the array and use the XOR operator “^” to XOR all the values.
- It is such that the same values are sorted and the XOR returns “0” on the same inputs therefore, the non-repeating value becomes fetched.
Output
In this outcome, it is evident that the non-repeating integer is fetched accordingly.
What is the Difference Between “XOR”, “Logical OR”, and “AND”?
XOR differs from the “Logical XOR” and “Logical AND” operators such that Logical OR retrieves “true” if either of the operands is “true”, and the AND operator returns “true” if all the operands are “true”. However, the XOR operator returns “true” only if all the operands are different.
Conclusion
“XOR” in Java is a bitwise operator that takes two boolean operands as input and returns “true” or “1” if they are different whereas on the same operands, it returns “false” or “0”. This operator is represented by the “^” symbol and is non-associative in nature. It can be used with the integer, string, and boolean types and is not compatible with the floating point numbers directly.