C Program to swap two variables by shifting bits
In this program two integer are swapped by shifting bits and 'ex-or' operation is performed till 'a' shifts to 'b' and vice-versa.
Program
#include<stdio.h>
void main()
{
int a, b, i, j;
printf("Enter two numbers\n");
scanf("%d %d", &a, &b);
for(i = 0; i < 16; i++)
{
if((a & (1 << i)) ^ (b & (1 << i)))
{
a = a ^ (1 << i);
b = b ^ (1 << i);
}
}
printf("Swapped numbers are: a = %d and b = %d\n", a, b);
}
Output
Enter two numbers
156
21
Swapped numbers are: a = 21 and b = 156