Javatpoint Logo
Javatpoint Logo

XOR Operation on Integers in Java

The XOR (exclusive OR) operation is a bitwise operation that compares corresponding bits of integers and returns a new integer wherein each bit is set if and best if one of the corresponding bits within the input integers is set. In this context, we will learn the XOR operation in Java, exploring its properties, programs, and provide a few examples.

Understanding the XOR Operation:

The XOR operation, represented by the caret symbol (^), is commonly used in programming for a variety of purposes, such as encryption, error detection, and manipulation of bits. It follows a specific set of conditions:

  • If both bits are zero, the result is zero.
  • If each bit is 1, the end result is zero.
  • If the bits fluctuate (one is zero and the alternative is 1), the result is 1.

XOR in Java

Java presents the XOR operator (^) to carry out XOR operations on integer values. The XOR operator can be used with each integer and boolean values.

XOR Operation on Integers

Let's explore some examples of XOR operations on integers in Java.

Example 1: Simple XOR Operation

XOROperationExample.java

Output:

6

In this example, we perform the XOR operation on integers a and b. The binary representation of a is 0101, and b is 0011. After applying the XOR operation, we get 0110, which is 6 in decimal representation.

Example 2: XOR for Toggle Operation

XOROperationExample.java

Output:

13

In this example, we use the XOR operation to toggle specific bits in an integer. We have an integer num with a binary representation of 1010. By performing XOR with toggle, which is 0111 in binary, we toggle the second and third bits, resulting in 1101, which is 13 in decimal.

Here's another example of using XOR for toggle operation:

XORToggleExample.java

Output:

11

Example 3: XOR Swap

XOROperationExample.java

Output:

a: 3, b: 5

This instance demonstrates how XOR may be used to change the values of variables without the use of a temporary variable. By applying XOR operations in collection, the authentic values of a and b are swapped.

In summary, The XOR operation on integers in Java provides a versatile way to manipulate bits, perform toggling operations, and even swap variable values. Understanding and utilizing XOR operations can enhance your programming skills, especially when working with low-level bit manipulation or cryptographic algorithms. By applying the XOR operator strategically, you can achieve efficient and concise code. In this article, we explored the XOR operation in Java, understanding its principles, and delving into various examples. By experimenting with different XOR operations, you could find out its flexibility and strength in solving a huge variety of programming challenges. Remember, the XOR operation is simply one of the many beneficial bitwise operations to be had in Java, and a deeper knowledge of bitwise operations can considerably expand your programming capabilities.







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