PROGRAM TO SWAP TWO NUMBERS USING BITWISE XOR
In this example, you will learn a program to swap the contents of two numbers using Bitwise XOR operation.
Program:
//Program to swap the content of two numbers using Bitwise Xor operation
#include<stdio.h>
int main( )
{
long i, k;
printf("Enter two integers: \n");
scanf("%ld %ld", &i, &k);
printf("\n Before swapping i= %ld and k = %ld", i, k);
i = i ^ k;
k = i ^ k;
i = i ^ k;
printf("\n After swapping i= %ld and k = %ld", i, k);
return 0;
}
#include<stdio.h>
int main( )
{
long i, k;
printf("Enter two integers: \n");
scanf("%ld %ld", &i, &k);
printf("\n Before swapping i= %ld and k = %ld", i, k);
i = i ^ k;
k = i ^ k;
i = i ^ k;
printf("\n After swapping i= %ld and k = %ld", i, k);
return 0;
}
Enter two integers:
71
90
Before swapping i= 71 and k = 90
After swapping i= 90 and k = 71
71
90
Before swapping i= 71 and k = 90
After swapping i= 90 and k = 71
So that's it ! Just try this code by your own. If you have any doubts then feel free to drop a comment . I'll be happy to answer your questions .
Keep coding....
Comments
Post a Comment