C Bitwise XOR Operator

In C, the Bitwise XOR ^ operator is used to perform a bitwise exclusive OR operation between two numbers. It compares corresponding bits of two operands and returns 1 if the bits are different and 0 if they are the same.

The Bitwise XOR operator is commonly used in encryption, swapping values without a temporary variable, and bit manipulation.


Syntax of the Bitwise XOR Operator

The syntax to use the Bitwise XOR operator is:

</>
Copy
result = operand1 ^ operand2;

Explanation:

  1. operand1: The first operand in the XOR operation.
  2. ^: The bitwise XOR operator.
  3. operand2: The second operand in the XOR operation.
  4. result: Stores the output of the XOR operation, which is calculated as follows:
    • If corresponding bits are different → result is 1
    • If corresponding bits are the same → result is 0

Examples of the Bitwise XOR Operator

1. Basic Example of Bitwise XOR

In this example, we will perform a Bitwise XOR operation between two integers and print the result.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int a = 5, b = 3;
    int result = a ^ b;

    printf("Bitwise XOR of %d and %d is %d\n", a, b, result);
    return 0;
}

Explanation:

  1. We declare two integers a and b with values 5 and 3.
  2. We perform the XOR operation a ^ b and store the result in result.
  3. The binary representation of 5 is 101 and of 3 is 011.
  4. Performing bitwise XOR:
            101
        XOR 011
        --------
            110 (which is 6 in decimal)
  1. The program prints “Bitwise XOR of 5 and 3 is 6”.

Output:

Bitwise XOR of 5 and 3 is 6

2. Swapping Two Numbers Using XOR

In this example, we will swap two numbers using the XOR operator without using a temporary variable.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int x = 10, y = 20;

    printf("Before swapping: x = %d, y = %d\n", x, y);

    x = x ^ y;
    y = x ^ y;
    x = x ^ y;

    printf("After swapping: x = %d, y = %d\n", x, y);

    return 0;
}

Explanation:

  1. We declare two integer variables x and y with values 10 and 20.
  2. We use XOR operations to swap values without a temporary variable:
    • x = x ^ y: x now holds XOR of x and y.
    • y = x ^ y: y gets the original x value.
    • x = x ^ y: x gets the original y value.
  3. We print the values of x and y before and after swapping.

Output:

Before swapping: x = 10, y = 20
After swapping: x = 20, y = 10

3. Checking if Two Numbers are the Same Using XOR

In this example, we will use XOR to check if two numbers are equal.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num1 = 7, num2 = 7;

    if ((num1 ^ num2) == 0) {
        printf("Numbers are equal.\n");
    } else {
        printf("Numbers are not equal.\n");
    }

    return 0;
}

Explanation:

  1. We declare two integer variables num1 and num2 with the same value 7.
  2. We use num1 ^ num2, which results in 0 if both numbers are equal.
  3. If the result is 0, the program prints “Numbers are equal.” Otherwise, it prints “Numbers are not equal.”

Output:

Numbers are equal.

Conclusion

In this tutorial, we covered the Bitwise XOR ^ operator in C:

  1. It returns 1 if bits differ and 0 if they are the same.
  2. It is used in swapping values, encryption, and checking equality.