XOR Swap still viable algorithm?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 1,430
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 232
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

XOR Swap still viable algorithm?

 
0
  #1
Aug 9th, 2007
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
  1. int first = 10;
  2. int second = 5;
  3. cout<<"First: "<< first <<"\nSecond: "<< second << endl;
  4. // XOR Swap
  5. first ^= sec;
  6. sec ^= fir;
  7. fir ^= sec;
  8. // Values are now switched
  9. cout<<"First: "<< first <<"\nSecond: "<< second << endl;

It also works with a char array
  1. cout<<" XOR Swap"<< endl;
  2. cout<<"Before Swap:"<< endl;
  3.  
  4. char x[6] = "shawn";
  5. char y[6] = "cplus";
  6.  
  7. cout<<"X: "<< x << endl;
  8. cout<<"Y: "<< y << endl;
  9. // XOR Swap
  10. for(int i=0;i<7;i++)
  11. x[i]= x[i] ^ y[i];
  12. for(int i=0;i<7;i++)
  13. y[i]= x[i] ^ y[i];
  14. for(int i=0;i<7;i++)
  15. x[i]= x[i] ^ y[i];
  16.  
  17. cout<<"\nAfter Swap:"<< endl;
  18. cout<<"X: "<< x << endl;
  19. cout<<"Y: "<< y << endl;
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,653
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: XOR Swap still viable algorithm?

 
0
  #2
Aug 9th, 2007
>>What do you guys/gals think
I hate it because it does nothing more than obfuscate the program. Maintainability and readability are more important than cuteness.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: XOR Swap still viable algorithm?

 
0
  #3
Aug 9th, 2007
the xor swap algorithm fails if you try to swap a variable with itself.
other swap algorithms using arithmetic operators fail if overflow or underflow occurs.
the correct way to swap two variables a and b of any type is std::swap(a,b). we would expect std::swap to be correct and to have the most efficient implementation possible for that type.
to me, std::swap is the coolest of the lot.
Last edited by vijayan121; Aug 9th, 2007 at 11:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 31
Reputation: mcldev is an unknown quantity at this point 
Solved Threads: 2
mcldev mcldev is offline Offline
Light Poster

Re: XOR Swap still viable algorithm?

 
0
  #4
Aug 10th, 2007
XOR is a assembler command. So... it executes very fast. Inside of C++ or worse C#, it will be a translation but you in C++, at least it will translate directly.

Compared to swapping by using 3 variables, this is akin to MOV operations on the processor which are generally more expensive than XOR operations.

But here's the deal by the time you program in C++ using XOR is not recommended unless you are performing special bitwise operations. The newer safer functions are highly optimized and can get the swapping job done more easily (less code).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3012 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC