I know about these. Is there any other ways as well? And which one is better?
- Using temporary Variables
- Using some Arithmetic operation
- Using bitwise XOR operation
I believe the best approach is the XOR implementation. Any suggestions?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Anything that doesnt use temporary variable/register is guaranteed not to work always. Hence NOT advised to use it.
Bitwise XOR swap doesnt work on same variable.(http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice)
‘Arithmetics without temp variable swap’ will have overflow issues.
Also note that if you do a simple swap using temporary, compiler is smart enough to convert it to assembly without using temp variable. If you try to over-smart it, then