Open In App

Bitwise XOR Operator in Programming

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.

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 = operand1 ^ operand2;

Bitwise XOR operator in 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++:




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




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:




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




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:




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:

Applications of Bitwise XOR Operator:

Here are some applications of the bitwise XOR operator:


Article Tags :