CODE OF GEEKS


A PLATFORM TO LEARN | CODE | ACHIEVE


" Programming is the real test of your creativity,its all about how you turn ideas into codes. "



Connect :


THIS WEBSITE IS BEST VIEWED IN THE GOOGLE CHROME WEB BROWSER.



TO SWAP TWO NUMBERS USING XOR OPERATION IN C

Code

#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2;
printf("ENTER TWO NUMBERS \n");
scanf("%d %d",&num1,&num2);
printf("FIRST NUMBER = %d \n",num1);
printf("SECOND NUMBER = %d \n",num2);
num1=num1^num2;
num2=num1^num2;
num1=num1^num2;
printf("SO,AFTER SWAPPING :\n");
printf("FIRST NUMBER = %d \n",num1);
printf("SECOND NUMBER = %d \n",num2);
return 0;
}

Output


ENTER TWO NUMBERS 10 20
FIRST NUMBER = 10
SECOND NUMBER = 20
SO,AFTER SWAPPING :
FIRST NUMBER = 20
SECOND NUMBER = 10


What we did - Our Approach


In this problem,we are supposed to swap the values of two numbers using xor operation.Swapping means interchanging of values among variables,swapping using xor operation takes place in three processes :
Let us assume that num1=20 num2=10 so,

Process 1 : num1=num1^num2 num1=20^10 => 30 Process 2 : num2=num1^num2 num2=30^10 => 20 Process 3 : num1=num2^num1; num1=20^30 => 10 Now num1=10 & num2=20 ,hence numbers are now swapped.


CODE OF GEEKS

A Platform to LEARN | CODE | ACHIEVE

Connect :



INTERESTING READ FOR YOU :

SHARE:



For any Queries,suggestions,feel free to Mail us at :
codeofgeeks@gmail.com

For reporting any kind of copyright voilation,mail us at : cogcopyclaim@gmail.com

© copyright CODE OF GEEKS 2018