One of the very tricky questions asked in an interview.
Swap the values of two variables like a=10
and b=15
.
Generally to swap two variables values, we need 3rd variable like:
temp=a;
a=b;
b=temp;
Now the requirement is, swap values of two variables without using 3rd variable.
single line solution for swapping two values in c language.
Consider
a=10
,b=15
:that's the correct XOR swap algorithm
you have to ensure that memory locations are different and also that the actual values are different because A XOR A = 0
It's very simple, but it may raise a warning.