# C program to SWAP two numbers without using temporary variable | Swapping of two numbers using bit-wise operator with XOR operator | Understanding of XOR operator

### /********* PROGRAM **********/

#include<stdio.h>
main()
{
int a,b;
printf(“Enter a,b values : “);
scanf(“%d%d”,&a,&b);
printf(“Before swapping a = %d t b= %d n”,a,b);
a^=b^=a^=b; //actual operation if you don’t understand, see below explanation
printf(“Before swapping a = %d t b= %d n”,a,b);
}

### Explanation :

a^=b^=a^=b is equal to
a = a^b;
b = b^a;
a = a^b;
Here ^ is bit-wise XOR operator

Let’s take one example to illustrate this logic. before going to example just have a look on bit-wise XOR truth table

 bit a bit b a ^ b (a XOR b) 0 0 0 0 1 1 1 0 1 1 1 0

Let us assume two values a and b.
a=11, b=22;

a=a^b; (first operation)

00001011 (Binary Equivalent of 11)
00010110 (Binary Equivalent of 22)
———
00011101 (Result is stored in a now )
——–

b = b^a;
00010110
00011101
——–
00001011  (b value )
——–

a = a^b;
00011101
00001011
——–
00010110  (a value )
——–

Now a and b are swapped
a= 22 and b = 11..