XOR Binary Operator in Java
One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the specified boolean conditions cannot be true at the same time, the XOR operator is most useful.
We defined two values and used the XOR technique on them in the example above. Additionally, two XOR operations employing the conventional operator + have been added. So, to operate two XORs, we can write the conventional operators.
Thus, we can check for two criteria that cannot both be true, for instance, by using the XOR operator.
Let's think about the two scenarios X and Y. The values of X XOR Y are displayed in the following table:
X | Y | X XOR Y |
true | true | false |
true | false | true |
false | true | true |
false | false | false |
Let's think about the two scenarios A and B. The values of A XOR B are displayed in the afollowing table:
Java Bitwise Operators
A symbol defined to carry out a certain operation is called an operator. For instance, two values can be added using the operator "+." Java offers bitwise operators the same support it does for conventional operators. These operators are employed to carry out operations on a number's individual bits. These operators are known as bitwise operators for this reason. It is assessed going right to left.
Any integral data type, including int, char, short, etc., can be utilized with the bitwise operators. When manipulating or querying the binary indexed tree, the bitwise operators are typically utilized.
In Java, there are seven different types of bitwise operators that are employed to carry out bit-level operations:
Operator | Description |
| | Bitwise OR |
& | Bitwise AND |
^ | Bitwise XOR |
~ | Bitwise Complement |
<< | Left shift |
>> | Signed Right Shift |
>>> | Unsigned Right shift |
Exclusive OR
The carrot () symbol denotes the XOR operator. If two values are provided, it returns true if they differ; else, it returns false. True is represented by 1 in binary, whereas false is represented by 0.
The XOR operator's truth table is shown below:
a | b | a^b |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 0 |
As we can see from the above table, it only returns true if the values of both operands differ. Otherwise, false is returned.
Let's use an illustration to clarify it:
TestXor.java
public class TestXor
{
public static void main(String[] args)
{
int a = 5, b = 7; // expressing values
// iterative XOR
// 0101 ^ 0111 = 0101 = 2
// performing a calculation using the conventional operator and xor
System.out.println("x ^ y = " + (x ^ y));
}
}
Output:
a ^ b = 2
We defined two values and used the XOR technique on them in the example above. Additionally, two XOR operations employing the conventional operator + have been added. So, to operate two XORs, we can write the conventional operators.
How Would Java Do That?
Let's now examine how the XOR operation is expressed in Java. Of course, we have the choice to utilize the && and || operators, but as we'll see, this can be a little wordy.
That's a little excessive, especially in light of the Java XOR operator that we have as a substitute, which is denoted by the symbol. It is a bitwise operator, which means that to produce a result, it compares the matching bits of two values. When using XOR, the outcome bit will be 0 if two bits in the same place have the same value. If not, it will be 1.
Last but not least, it's important to note that the XOR operator, like the other bitwise operators, supports all primitive types. For illustration, let's look at two integers, 1 and 3, whose respective binary representations are 00000001 and 000000011. The integer 2 is produced by using the XOR operator between them:
assertThat(1 ^ 3).isEqualTo(2);
declare that (1 + 3) is. These two values only differ in the second bit, hence the outcome of the XOR operator on this bit will be 1. The result of the bitwise XOR between the remaining bits, which are all equal, is 0, giving us the final value of 00000010, which is the binary representation of the number 2.EqualTo(2);