XOR Operator in C
In C programming, bitwise operators conduct different operations on the given data at the bit level. It operates only on two digits, 0 or 1, which are binary digits. Performing bitwise operations is also known as bitwise programming.
It is generally used to do fast calculations. It is similar to mathematics operations like addition, subtraction, multiplication, and division but is done at the bit level. Here, bit is not confused with byte (1 byte=8 bits).
Types of Bitwise Operators
Operator | Meaning |
& | AND |
| | OR |
^ | XOR |
- | COMPLIMENT |
<< | LEFT SHIFT |
>> | RIGHT SHIFT |
Bitwise XOR Operator
The Bitwise XOR or exclusive OR operator takes two sequences of binary digits (0 and 1) and performs a bitwise XOR operation on each pair.
In the XOR operation, if both sequences are opposite, then the resultant bit will be TRUE (1), and if both are the same, then the result will be FALSE (0). The XOR operator is denoted by the symbol (^).
Truth Table
x | Y | R=x ^ y |
0 | 1 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 0 |
In this, x= input 1
y= input 2
R= output
We have to perform XOR operation on two numbers, 11 and 23
Step 1: Converting 11 and 23 into binary digits (in the form of 0 and 1)
Binary digit of 11: 1011
Binary digit of 23: 10111
Step 2: Make both the inputs into equal 8-bit numbers by adding zero (0) to the left
11= 00001011
23= 00010111
Step 3: Performing XOR on both inputs
![XOR Operator in C](https://static.tutorialandexample.com/c/xor-operator-in-c1.png)
Example 1: XOR Program in C
Code
#include<stdio.h>
#include<conio.h>
int main()
{
int n1= 11, n2= 23, ans;
ans=n1^n2;
printf("XOR of 11 and 23 (11^23) = %d", ans);
return 0;
}
Output
![XOR Operator in C](https://static.tutorialandexample.com/c/xor-operator-in-c2.png)
Working of this code:
- This code is performing XOR on 11 and 23.
- We have n1=11, n2=23, and ans as output.
- The binary conversion of 11 and 23 is 1011 and 10111
- Then, the numbers are converted into 8-bit binary numbers by adding 0.
- Now, we will move from left to right on the binary digits and perform XOR, and the output will save to the variable ans.
- The first bit of 11 and 23 is 0 and 1, resulting in 1.
- The second bits are 1 and 0, which also give 1 as a result.
- Then the following bits are also opposite 0 and 1, so the resultant bit will be 1.
- The fourth bit of 11 and 23 is 1 and 1, which gives 0 as output.
- The last bits are also the same (1 and 1), which gives 0.
- The resultant binary value is 11100, equivalent to 23 in decimal numbers.
Let’s understand XOR by implementing some use cases using C language.
Example 2: Program to swap two numbers without a third variable in C
We can use the bitwise XOR operator to swap two numbers.
Pseudocode:
- Take 2 integer numbers x=10 and y=5
- Convert them into binary numbers.
- Perform XOR on both digits and save it into x.
- Then again, perform XOR and save it into y.
- And repeat the process and again save it into x.
Code
#include<stdio.h>
#include<conio.h>
int main()
{
int x=10, y=5; //binary of x=1010 and y=0101
x=x^y; // x becomes 15 (1111)
y=x^y; // y becomes 10 (1010)
x=x^y; // x becomes 5 (0101)
printf("After Swapping: x= %d, y= %d", x,y);
return 0;
}
Output
![XOR Operator in C](https://static.tutorialandexample.com/c/xor-operator-in-c3.png)
Working of this code:
- This code is swapping two numbers, x, and y.
- We have taken x as 10 and y as 5
- It then converted the number into its binary equivalent, which resulted in 1010 and 0101, respectively.
- Then we performed XOR, which gave us 15 (1111), and saved it into x.
- Then again, XOR is done with the new value of x and y and saved into y, which gives us y as 10 (1010)
- Then for the last time, we performed XOR on both the new values and the result was saved into x, which gives us 5 (0101).
- Now we swapped the values without using any other variable.
XOR with negative numbers
Let’s take two negative numbers to understand the working of the XOR operator with negative numbers.
- Take x as -4 and y as -2.
- Convert it into binary equivalent
-4 = 10000100
-2 = 10000010
The leftmost bit is already reserved for the signed value (thus 1 for the negative number)
- Convert the binary number into its complement.
- We have to reverse the number and add 1 to a negative number.
Complement of -4: 11111100
Complement of -2: 11111110
- Then we will perform XOR on both the complements to get XOR of -4 and -2.
![XOR Operator in C](https://static.tutorialandexample.com/c/xor-operator-in-c4.png)
- The resultant is equivalent to 2 in decimal
Code
#include<stdio.h>
#include<conio.h>
int main()
{
int n1=-4, n2=-2,ans;
ans=n1^n2;
printf("XOR of negative numbers -4 and -2 is: %d",ans);
return 0;
}
Output
![XOR Operator in C](https://static.tutorialandexample.com/c/xor-operator-in-c5.png)