As the title says: Is the XOR Swap algorithm still viable? I know that temp-swaps are supposedly faster but the shear coolness of an XOR swap makes it more attractive to me. What do you guys/gals think?
For anyone that doesn't know what an XOR swap is here goes
int first = 10;
int second = 5;
cout<<"First: "<< first <<"\nSecond: "<< second << endl;
// XOR Swap
first ^= sec;
sec ^= fir;
fir ^= sec;
// Values are now switched
cout<<"First: "<< first <<"\nSecond: "<< second << endl;
It also works with a char array
cout<<" XOR Swap"<< endl;
cout<<"Before Swap:"<< endl;
char x[6] = "shawn";
char y[6] = "cplus";
cout<<"X: "<< x << endl;
cout<<"Y: "<< y << endl;
// XOR Swap
for(int i=0;i<7;i++)
x[i]= x[i] ^ y[i];
for(int i=0;i<7;i++)
y[i]= x[i] ^ y[i];
for(int i=0;i<7;i++)
x[i]= x[i] ^ y[i];
cout<<"\nAfter Swap:"<< endl;
cout<<"X: "<< x << endl;
cout<<"Y: "<< y << endl;