×

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:

OperatorDescription
|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:

aba^b
000
011
101
11 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);


Related Topics

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Vigesimal in Java

A number system with a base of 20 is known as the vigesimal in Java. A base-20 (base-score) numeric system, often known as a vigesimal system, is centered on the twenty...

4 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

Java String toLowerCase() methods

Java String toLowerCase() method is used to convert all the characters of the String into lower case. Syntax: public String toLowerCase()              public String toLowerCase(Locale locale) Returns: It returns Lower...

1 minute read.

Java Math rint() Method

The rint() method of Java Math class returns the double value which is close to the specified argument and is equal to mathematical integer. Syntax: public static double rint(double a) Parameters: The parameter ‘a’...

1 minute read.

Centered Square Numbers in Java

In this tutorial, we will understand how to check the number is a centered square number. It is one of the prevalent interview questions of IT companies. Firstly, we will...

3 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

Java Generate Random String

Java Generate Random String In this tutorial, we will learn about to generate random string in Java. Random generation strings mean any string will be generated, which does not follow any...

7 minutes read.

Java Math.multiplyExact() method in Java

Java has an inbuilt math function called Math.multiplyExact() that returns the sum of the parameters. If the result exceeds an integer, an exception is thrown. There is no need to...

2 minutes read.

Objects and Classes in Java

Objects and Classes in Java Classes and objects are the basic concepts of object-oriented programming. It revolves around the real world entity. In Java, the object is a physical and logical...

5 minutes read.

Prepared statement in Java

Prepared statement: A prepared statement is a statement that is pre-compiled SQL statement, and it is a sub-interface of a statement. Compared to other statement objects in java, Prepared Statement objects have...

3 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

Java String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.