# Thread: We can swap two numbers using Xor operator.

1. ## We can swap two numbers using Xor operator.

I was searching for how to swap two numbers without temporary variable. Then I came across this blogpost.

He used xor operator to swap. I was amazed to see it.
Here is his program

Code:
```#include<stdio.h>
int main(int argc, char const *argv[]){
int number1,number2;
printf("\nSwapping of two numbers in c\nEnter two numbers\n");
scanf("%d%d",&number1,&number2);
printf("Number 1: %d\nNumber 2: %d",number1,number2);
//swapping
number1=number1^number2;
number2=number1^number2;
number1=number1^number2;
printf("\nAfter swapping");
printf("\nNumber 1: %d\nNumber 2: %d\n",number1,number2);
return 0;
}```

2. This is a nice bar bet, but I don't know any programmer that would use this in real life.

This can be more simplified by:
Code:
```number1 ^= number2;
number2 ^= number1;
number1 ^= number2;
// Or even:
number1 ^= number2 ^= number1 ^= number2;```
But again, I would never use it in real code.

3. > number1 ^= number2 ^= number1 ^= number2;
Not really...
Question 3.3b

> He used xor operator to swap. I was amazed to see it.
And if your data types are anything other than equal sized integers, you have to do it the proper way anyway.

You hung a spoon off the end of your nose - party trick for a 5-year old.