> ## 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.

# Introduction to XOR
- URL: https://www.javahandbook.com/bitmask/introduction-to-xor/
- Published: 2024-06-16T07:36:52.000Z
- Updated: 2025-08-07T03:21:08.000Z
- Author: Gopi Gorantala
- Tags: XOR Operator, #bitmask, #docs

The Bitwise XOR operator is denoted by ^. When an XOR gate is given with 2 inputs, the corresponding outputs will be: If two input bits are different, the output is 1\. In all other cases, it is 0.

This is an introductory lesson on XOR.

### Introduction

This operator is the same as the XOR gate that we studied in the digital electronics chapter, as shown below:

### Sketch

![Logic XOR Gate](https://storage.ghost.io/c/00/01/0001b1c8-8c52-4cdb-b3c7-c0746bea790e/content/images/2025/08/XOR-gate.svg)

Logic XOR Gate

### What is the Bitwise XOR operator?

The Bitwise XOR operator is denoted by ^. When an XOR gate is given with 2 inputs, the corresponding outputs will be:

- If two input bits are different, the output is 1.
- In all other cases, it is 0.

### Example:

- `1^1` \=> yields to `0`
- `0^0` \=> yields to `0`
- `1^0` \=> yields to `1`
- `0^1` \=> yields to `1`.

So Bitwise `^` returns a `1` in each bit position for which the corresponding bits of one of the operands are 1s.

### Syntax

```java
a^b
```

XOR compares each bit of the **first operand** to the **second operand**’s corresponding bit. If both bits are `1` or both bits are `0`, the corresponding result bit is set to `0`. Otherwise, the corresponding result bit is set to `1`.

### Bitwise `^` table

| a | b | a ^ b |
| - | - | ----- |
| 0 | 0 | 0     |
| 0 | 1 | 1     |
| 1 | 0 | 1     |
| 1 | 1 | 0     |

### Truth table

| a     | b     | a ^ b |
| ----- | ----- | ----- |
| False | False | False |
| False | True  | True  |
| True  | False | True  |
| True  | True  | False |

Let’s see some Bitwise `^` operator examples in the next lesson.