> ## Content Index
> Fetch the complete content index at: https://www.javahandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Bitwise XOR, Computations, and Examples
- URL: https://www.javahandbook.com/bitmask/bitwise-xor-computations-and-examples/
- Published: 2024-06-16T07:47:51.000Z
- Updated: 2024-06-17T03:42:28.000Z
- Author: Gopi Gorantala
- Tags: XOR Operator, #bitmask, #docs

## Introduction

A Bitwise **XOR** is a binary operation that takes two-bit patterns of equal length and performs the logical exclusive OR operation on each corresponding bits pair. Each position’s result is 0 if both bits are `0` or if both bits are `1`. Otherwise, the result is 1.

## Bitwise XOR

The Bitwise **XOR** operator does the following:

- It is a binary operator that takes two numbers.
- When we do Bitwise `^` of these two numbers, it considers the binary representation of these two numbers.
- Bitwise XOR compares bit by bit in binary representation, and whatever binary representation you get, the corresponding Integer is returned as an output.

The integer data type takes `4` bytes in C, C++, and Java programming languages.

```json
4 Bytes = 4 * (8 bits) = 32 bits. {1 Byte = 8 bits}
```

For simplicity, we’ll represent these in 16-bit instead of 32-bit binary.

```json
a = 12
b = 10
---------------------------------
a in binary : 0000 0000 0000 1100
b in binary : 0000 0000 0000 1010
---------------------------------
a ^ b       : 0000 0000 0000 0110
---------------------------------
```

The `^` operation happens from the rightmost bit traversing towards the leftmost bit in the 32-bit binary representation.

### Illustration

Let’s visualize the steps below:

Step #1:

```json
a = 12
b = 10
---------------------------------
a in binary : 0000 0000 0000 1100
b in binary : 0000 0000 0000 1010
---------------------------------
a ^ b       :                   0
---------------------------------
```

Step #2:

```json
a = 12
b = 10
---------------------------------
a in binary : 0000 0000 0000 1100
b in binary : 0000 0000 0000 1010
---------------------------------
a ^ b       :                  10
---------------------------------
```

Step #3:

```json
a = 12
b = 10
---------------------------------
a in binary : 0000 0000 0000 1100
b in binary : 0000 0000 0000 1010
---------------------------------
a ^ b       :                 110
---------------------------------
```

Step #4:

```json
a = 12
b = 10
---------------------------------
a in binary : 0000 0000 0000 1100
b in binary : 0000 0000 0000 1010
---------------------------------
a ^ b       : 0000 0000 0000 0110
---------------------------------
```

### Code

Below is the code representation of the `^` operator.

### Java

```java
class XOROperation {
    public static void main( String args[] ) {
        int x = 12;
        int y = 10;
        System.out.println("Bitwise XOR of (x , y) is : " + (x ^ y)); // yields to 6
    }
}
```

### Python

```py
x=12
y=10
print("Bitwise XOR of (x,y) is : ",(x^y))
```

### JavaScript

```js
const XOROperation = (x, y) => x ^ y;

const x = 12;
const y = 10;

console.log (`Bitwise XOR of (x , y) is : ${XOROperation (x, y)}`);

```

### C++

```cpp
#include <iostream>

using namespace std;

int main() {
    int x = 12, y = 10;
    cout << "Bitwise XOR of (x,y) is : " << (x ^ y);
    return 0;
}
```

### TypeScript

```ts
const XOROperation = (x: number, y: number): number => x ^ y;

const x: number = 12;
const y: number = 10;

console.log(`Bitwise XOR of (x , y) is : ${XOROperation(x, y)}`);

```

In the following lessons, we will apply the Bitwise approach to solve a few problems with XOR.

Let’s jump into our first problem using the Bitwise `^` operator in the next lesson.