×

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

Automatic Resource Management

In computer programming, resource management refers to the wide set of techniques for the effective management of the system resources. There are two ways of management of resources: The computer program itself...

2 minutes read.

Lazy loading in Java

Lazy loading is the idea of waiting to load an object until you need it. In other words, it is the practice of postponing class instantiation until it is necessary....

5 minutes read.

Java Integer reverse() method

The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation. Syntax public static int reverse (int i)  Parameters The parameter...

1 minute read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Java Integer lowestOneBit()

The lowestOneBit () method of Java Integer class returns an int value with at most a single one-bit, in the position of the lowest-order one-bit in the specified int value.  Syntax public...

2 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Java Calculate Average of List

The list is a linear data structure used in Java to store ordered data collections. Additionally, it accepts duplicate values while maintaining insertion order. It is sometimes necessary to find...

3 minutes read.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Java Boolean compareTo() method

The compareTo() method of Java Boolean class compares the Boolean argument with the Boolean instance and returns integer value, zero, or negative 1, or positive 1 based on the result...

2 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Creating API Document Javadoc tool

The JavaDoc utility is a document generator tool written in Java that generates standard documentation in HTML format. It parses declarations and documentation in a source file collection that describes...

3 minutes read.

Java Math ulp() Method

The ulp() method of Java Math class returns the size of an ulp of the argument. Syntax: public static double ulp(double x)public static float ulp(float x) Parameters: The parameter ‘x’ represents the floating-point number...

2 minutes read.

Java Math floorDiv() Method

The floorDiv () method of Math class returns the largest integer value that is less than or equal to the algebraic quotient. It firstly divides the dividend and divisor and...

2 minutes read.

Java String contains() method

contains() method returns true only if it contains the given sequence of characters otherwise it returns false. syntax: public boolean contains(CharSequence sequence) parameters: sequence : It is the sequence to be searched. Returns: It returns true...

1 minute read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.