Javatpoint Logo
Javatpoint Logo

XOR Operations in Java

Logical calculations and programming both depend on XOR (exclusive OR) operations. The XOR operator in Java offers a quick and easy way to work with binary data and perform bitwise operations. The specifics of XOR operations in Java will be covered in full in this section, along with examples and a step-by-step tutorial to make them easier to comprehend and use.

XOR Operation in Java

The Java "^" symbol represents the XOR operation, a bitwise operator that compares the corresponding bits of two operands. If just one of the matching bits in the operands is set, it returns a new value with all of the bits set.

The XOR operation in this example compares the bits in each place of a and b. The result's corresponding bit is set to 1 if the bits are distinct; otherwise, it is set to 0.

Java XOR Operation

You may use the XOR operator "^" directly in your Java code to conduct XOR operations. Here is a program that uses XOR operations as an example:

XORExample.java

Output:

6

Example 1: Using Java's XOR operator

The following code sample demonstrates how to use the XOR operator in Java:

We have two boolean variables with true and false values in this example. The XOR operator is used to conduct a logical exclusive OR operation between them, and the result is stored in another boolean variable c. The programme returns "true" because only one of the operands is true and the other is false.

Example 2: Using the XOR operator in Java to compare two values

The XOR operator compares two values or conditions where only one of them can be true. Take a look at the following code example:

In this example, we have two integer variables, x and y, with values of 10 and 20, respectively. The XOR operator is used to compare two conditions: (x > 0) and (y 30). Because just one of the requirements is true, the outcome of the XOR operation is true.

Example 3: Using the XOR operator in Java to check for odd or even numbers

The XOR operator can be used to determine if an integer is odd or even. In Java, the least significant bit of every even integer is 0, whereas the least significant bit of every odd integer is 1. We may utilise this integer attribute to determine whether a number is odd or even by using the XOR operator. Consider the following line of code:

In this example, the integer variable n has the value 7. To conduct a logical exclusive OR operation between n and 1, we utilise the XOR operator. If the XOR procedure yields n + 1, then n is an odd number; otherwise, it is an even number. The programme returns, "7 is an odd number."

1. 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 result is the number's parity (in this example, 0 for even).

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

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







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