Last Updated : 27 Feb, 2024

Improve

** Bitwise XOR Operator **is represented by the caret symbol (^). It is used to perform a bitwise XOR operation on the individual bits of two operands. The XOR operator returns 1 if the corresponding bits in the two operands are different, and 0 if they are the same.

Table of Content

- What is Bitwise XOR?
- Bitwise XOR operator:
- Bitwise XOR operator in C:
- Bitwise XOR operator in C++:
- Bitwise XOR operator in Java:
- Bitwise XOR operator in Python:
- Bitwise XOR operator in C#:
- Bitwise XOR operator in Javascript:
- Use Cases of Bitwise XOR Operator:
- Applications of Bitwise XOR Operator in Programming:

__What is Bitwise XOR?__

Bitwise XOR (exclusive OR) is a binary operation that takes two equal-length binary representations and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if only one of the two bits is 1 but will be 0 if both are 0 or both are 1.

The truth table for the XOR (exclusive OR) operation is as follows:

A | B | A XOR B |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

In this table, A and B are the variables, and A XOR B is the expression representing the logical XOR operation. The table shows all possible combinations of truth values for A and B, and the resulting truth value of A XOR B for each combination.

The XOR operation returns 1 (true) if the inputs are different, and 0 (false) if they are the same. This is why the output is 1 for the second and third rows, where A and B have different values, and 0 for the first and fourth rows, where A and B have the same value.

__Bitwise XOR operator:__

The bitwise XOR operator is represented by the caret symbol (^) in many programming languages, including Python, C, C++, and Java.

result = operand1operand2;^

__Bitwise XOR operator in C:__

## C

`#include <stdio.h>`

`int`

`main()`

`{`

`int`

`a = 10; `

`// 1010 in binary`

`int`

`b = 6; `

`// 0110 in binary`

`int`

`result = a ^ b; `

`// 1100 in binary`

`printf`

`(`

`"%d\n"`

`, result); `

`// Output: 12`

`return`

`0;`

`}`

**Output**

12

__Bitwise XOR operator in C++:__

## C++

`#include <iostream>`

`using`

`namespace`

`std;`

`int`

`main()`

`{`

`int`

`a = 10; `

`// 1010 in binary`

`int`

`b = 6; `

`// 0110 in binary`

`int`

`result = a ^ b; `

`// 1100 in binary`

`cout << result << endl; `

`// Output: 12`

`return`

`0;`

`}`

**Output**

12

__Bitwise XOR operator in Java:__

## Java

`import`

`java.io.*;`

`class`

`GFG {`

`public`

`static`

`void`

`main(String[] args)`

`{`

`int`

`a = `

`10`

`; `

`// 1010 in binary`

`int`

`b = `

`6`

`; `

`// 0110 in binary`

`int`

`result = a ^ b; `

`// 1100 in binary`

`System.out.println(result); `

`// Output: 12`

`}`

`}`

**Output**

12

__Bitwise XOR operator in Python: __

## Python3

`a `

`=`

`10`

`# 1010 in binary`

`b `

`=`

`6`

`# 0110 in binary`

`result `

`=`

`a ^ b `

`# 1100 in binary`

`print`

`(result) `

`# Output: 12`

**Output**

12

__Bitwise XOR operator in C#:__

## C#

`using`

`System;`

`class`

`MainClass {`

`public`

`static`

`void`

`Main(`

`string`

`[] args)`

`{`

`int`

`a = 10; `

`// 1010 in binary`

`int`

`b = 6; `

`// 0110 in binary`

`int`

`result = a ^ b; `

`// 1100 in binary`

`Console.WriteLine(result); `

`// Output: 12`

`}`

`}`

**Output**

12

__Bitwise XOR operator in Javascript:__

## Javascript

`let a = 10; `

`// 1010 in binary`

`let b = 6; `

`// 0110 in binary`

`let result = a ^ b; `

`// 1100 in binary`

`console.log(result); `

`// Output: 12`

**Output**

12

__Use Cases of Bitwise XOR Operator__:

The bitwise XOR operator (^) has several use cases in programming:

: The XOR operator can be used to flip individual bits in a number. For example,**Flipping individual bits**`x ^ 1`

will flip the least significant bit of`x`

.: The XOR operator can be used to swap the values of two variables without using a temporary variable. For example,**Swapping two variables without a temporary variable**`a ^= b; b ^= a; a ^= b;`

will swap the values of`a`

and`b`

.: The XOR operator can be used to check if two numbers have opposite signs. For example,**Checking if two numbers have opposite signs**`(x ^ y) < 0`

will return true if`x`

and`y`

have opposite signs.: The XOR operator can be used to implement simple encryption algorithms. For example,**Implementing simple encryption algorithms**`x ^ key`

will encrypt`x`

using`key`

, and`x ^ key ^ key`

will decrypt`x`

using`key`

.: The XOR operator can be used to detect changes in data. For example,**Detecting changes in data**`x ^ y`

will return 0 if`x`

and`y`

are the same, and a non-zero value if they are different.

__Applications of Bitwise XOR Operator:__

Here are some applications of the bitwise XOR operator:

: XOR operations are used in error detection and correction codes like CRC (Cyclic Redundancy Check) and Hamming codes. By XORing the data with a known pattern, errors in transmission can be detected and corrected.**Error detection and correction**: XOR operations can be used in pseudo-random number generation algorithms. By XORing a number with itself and a shifting pattern, a sequence of seemingly random numbers can be generated.**Random number generation**: XOR operations are used in parity checking to determine if the number of bits set to 1 in a binary word is odd or even. This is often used in computer memory systems to detect errors.**Parity checking**: XOR operations can be used in image processing to perform operations like image blending, edge detection, and watermarking.**Image processing**: XOR operations are used in digital signal processing algorithms like Fast Fourier Transform (FFT) and convolution to perform operations on digital signals.**Digital signal processing**: XOR operations are used in cryptographic hash functions like MD5 and SHA-1 to create a unique hash value for a given input.**Cryptographic hash functions**: XOR operations are used in network protocols like TCP and UDP to calculate checksums for data packets to ensure data integrity during transmission.**Network protocols**

srinam

Improve

Next Article

Bitwise OR Operator (|) in Programming