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:
result = operand1 ^ operand2;
Explanation:
operand1
: The first operand in the XOR operation.^
: The bitwise XOR operator.operand2
: The second operand in the XOR operation.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
- If corresponding bits are different → result is
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
#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:
- We declare two integers
a
andb
with values 5 and 3. - We perform the XOR operation
a ^ b
and store the result inresult
. - The binary representation of 5 is
101
and of 3 is011
. - Performing bitwise XOR:
101
XOR 011
--------
110 (which is 6 in decimal)
- 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
#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:
- We declare two integer variables
x
andy
with values 10 and 20. - We use XOR operations to swap values without a temporary variable:
x = x ^ y
:x
now holds XOR ofx
andy
.y = x ^ y
:y
gets the originalx
value.x = x ^ y
:x
gets the originaly
value.
- We print the values of
x
andy
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
#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:
- We declare two integer variables
num1
andnum2
with the same value 7. - We use
num1 ^ num2
, which results in 0 if both numbers are equal. - 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:
- It returns
1
if bits differ and0
if they are the same. - It is used in swapping values, encryption, and checking equality.