Swap two numbers without using third variable and using XOR Operator in C Programming

January 10, 2025 No Comments » Hits : 240





Generally Swaping two number requires three variables , Let’s Take look at Procedure of swaping two Number
For Swaping Two numbers following procedure is used -

x = x ^ y --> x^=y -- (1)
y = y ^ x --> y^=x -- (2)
x = x ^ y --> x^=y -- (3)

Now we will Explaining above three statements using example ….
Let x = 12 and y = 9 [ For our sake and simplicity consider number is of 4 bits ]

x = 1100
y = 1001

X-OR Table :

A B A X-OR B
110
101
011
000

Step 1 : After : x = x ^ y

x   = 1100
y   = 1001
----------
x^y = 0101
----------
x   = 0101    ..... New Value of x

Step 2 : After y = y ^ x

x   = 0101    ..... New Value is taken
y   = 1001    ..... Old Value of Y
----------
y^x = 1100
----------
y   = 1100    ..... New Value of y = Initial x

Step 3 : After x = x ^ y

x   = 0101    ..... New Value from step 1
y   = 1100    ..... New Value of y from Step 2
----------
y^x = 1001
----------
x   = 1001    ..... New Value of x = Initial y

Here is Program for : [Swap / Interchange two variables [numbers] without using Third Variable]

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
printf("nEnter First Number : ");
scanf("%d",&num1);
printf("nEnter Second Number : ");
scanf("%d",&num2);
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
printf("\nNumbers after Exchange : ");
printf("num1 = %d and num2 = %d",num1,num2);
getch();
}

Output :

Enter First Number : 20
Enter Second Number : 40
Numbers after Exchange : num1 = 40 and num2 = 20

Incoming search terms: