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);
}
/********** OUTPUT ***************/
Explanation :
bit a
|
bit b
|
a ^ b (a XOR b)
|
0
|
0
|
0
|
0
|
1
|
1
|
1
|
0
|
1
|
1
|
1
|
0
|