Javatpoint Logo
Javatpoint Logo

XOR Binary Operator in Java

The binary operator XOR (Exclusive OR) is a basic operation in computer programming, including Java. It is an arithmetic operator that performs a bitwise exclusive OR operation on two operands of the same data type and returns a new value based on the outcome. In this section, we will discuss the XOR binary operator in depth, including its syntax, uses, and examples.

The truth table of the XOR binary operator illustrates the result of the operation for all conceivable combinations of two inputs. The truth table for XOR is as follows:

Input A Input B Output
0 0 0
0 1 1
1 0 1
1 1 0

Associativity

Because the XOR binary operator in Java is not associative, the order in which it is applied to multiple operands might alter the outcome. (a b) c, for example, is not always equivalent to a (b c).

Bitwise XOR and Logical XOR

Java has two kinds of XOR operators: bitwise XOR and logical XOR. The binary representation of two operands is used in bitwise XOR, but the boolean values are used in logical XOR.

Java XOR Operator Syntax:

In Java, the XOR operator is represented by the caret symbol (^). It has the following syntax:

In this case, operand1 and operand2 can be any integer type expressions that evaluate to the same data type value.

Based on the outcome of the operation, the XOR operator returns a new value. The output is an integer of the same type as the operands.

Uses of XOR Operator

  1. The XOR operator is most commonly employed in bitwise operations, comparing the corresponding bits of two operands and setting the resultant bit to 1 if only one of the corresponding bits is 1. If both matching bits are 0, the outcome is 0.
  2. The XOR operator is also employed in encryption and decryption. It is a fundamental component of cryptographic methods that employ XOR to safeguard data manipulation. It is also employed in error detection, correction, data compression, and digital signal processing.
  3. The operator is also used to check parity bits.
  4. In Java, XOR may be used to check for a number's parity. A number's parity is whether it has an even or odd number of 1s in its binary form.

Explanation

XOR is used in this example to compute the parity of the integer 6. The while loop iterates through the integer, XORing each bit with the parity variable. The end result is the number's parity (in this example, 0 for even).

5. Using XOR to Swap the Values of Two Variables (alternate technique): An alternate technique to the one discussed previously may be used to swap the values of two variables in Java.

XOR is utilised in this example to swap the values of a and b without the usage of a temporary variable by employing a different order of XOR operations.

6. Toggling a Bit with XOR

In Java, XOR may be used to toggle a specific bit in a number.

Example:

Consider a basic example to demonstrate how to utilise the XOR operator in Java. Consider two numbers, a = 10 and b = 5. These integers are represented in binary as follows:

a = 1010 b = 0101

We just use the caret (^) symbol to execute the XOR operation on these integers:

int outcome = a^ b;

This procedure yielded the following results:

1010 0101 = 1111 (decimal value 15) is the result.

The XOR operator in this example compared the corresponding bits of both operands and set the resultant bit to 1 if only one of the corresponding bits was 1. The resultant number, 1111, is the binary equivalent of the decimal value 15.

XorExample.java

Output:

a = 12
b = 7
a ^ b = 11

Conclusion

Finally, the XOR binary operator is a basic computer programming operation that is frequently used in Java for bitwise operations, encryption and decryption, error detection and repair, data compression, and digital signal processing. Its syntax is straightforward, with the operator represented by the caret (). Programmers may alter data in a secure and efficient manner by knowing the XOR operator, making it a fundamental component of contemporary computing.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA