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;
}



Output:

Enter two integers:
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

Popular posts from this blog

DECIMAL TO OCTAL CONVERSION IN C

PROGRAM TO CONVERT BINARY CODE TO GRAY CODE

KRISHNMURTHY NUMBER PROGRAM IN C