Please explain how the swapping is done here

Hello @kumarpratikkant,
In this the swapping is done without using temporary variable and using bitwise XOR operation which is ^ operator.
The XOR of two numbers x and y returns a number that has all the bits as 1 wherever bits of x and y differ.
For example:
Initially
x=10 bit representation: 1010
y=5 bit representation:0101
Now
x = x ^ y x now becomes 15 (1111)
y = x ^ y y becomes 10 (1010)
x = x ^ y x becomes 5 (0101)
Afterwards
So you see using bitwise XOR(^) operator we have made x=5 and y=10.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.