| | |
XOR Swap still viable algorithm?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
It also works with a char array
For anyone that doesn't know what an XOR swap is here goes
c++ Syntax (Toggle Plain Text)
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
c++ Syntax (Toggle Plain Text)
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;
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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.
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.
•
•
Join Date: Feb 2005
Posts: 31
Reputation:
Solved Threads: 2
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).
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).
![]() |
Similar Threads
- convert int to string (C)
- swapping (C)
- plz help me to do this(new) (C++)
- muliplying without using a * operator! (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ Project
- Next Thread: How do I resolve this ambiguous call?
Views: 3012 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






