Javatpoint Logo
Javatpoint Logo

XOR Logical Operator Java

The " ^ " symbol in Java represents the XOR logical operator, which performs a logical exclusive OR operation between two boolean values. If one of the operands is true and the other is false, this operator returns true; otherwise, it returns false.

The XOR operator is a typical logical operator in programming that is useful for comparing two values or conditions when only one may be true. This post will look at the XOR operator in-depth and show how it may be used in Java.

Syntax:

Where operand1 and operand2 are boolean values, the result is also a Boolean value.

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.

Conclusion

In Java, the XOR operator is a strong logical operator that may be used to conduct logical exclusive OR operations on two boolean values. It is especially handy when comparing two values or situations where only one may be true. It may also be used to determine whether a given number is odd or even.







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