468,512 Members | 1,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Swap two int variables without using any temporary variable

In a C++ quiz, we were asked to write a program to swap two integer variables without using a third temporary variable.

Any suggestions??
Nov 28 '07 #1
9 7481
Meetee
931 Expert Mod 512MB
In a C++ quiz, we were asked to write a program to swap two integer variables without using a third temporary variable.

Any suggestions??
You can apply some logic like this:

b = a + b;
a = b - a;
b = b - a;

There may be other ways too.

Regards
Nov 28 '07 #2
oler1s
671 Expert 512MB
Look up XOR swap.

I don't recommend using these swapping tricks though. The most optimal swapping algorithm is implicitly swapping the references to the variables, which a modern compiler will most likely do. You can't beat that with any gimmick you come up with.
Nov 28 '07 #3
Thank you very much but the one method which oler1s mentions, Does it use a temp var or not??
Nov 29 '07 #4
oler1s
671 Expert 512MB
Well, what do you think, given that I was answering your question?

Plus, why didn't you actually look it up and see for yourself? You would know whether XOR swaps use temporary variables or not just by seeing the method online.
Nov 29 '07 #5
Please tone down. You have got me wrong. Thank you for the Swap Code . I got the code from the net. It works in the same way as the Add-swap does except it does the swap with bit operations.

Actually my query was about what you mentioned as ...

"The most optimal swapping algorithm is implicitly swapping the references to the variables, which a modern compiler will most likely do."
Nov 29 '07 #6
I would like you to focus on this point:there is not so much advantage in using XOR swap,consider this code written in MIPS/DLX assembly
[code]
lw $t1,BaseA(0) //we assume that our variables a,b are at postion
lw $t2,BaseB(0) // Mem[BaseA+0] and Mem[BaseB+0] respectively
Nov 29 '07 #7
so we could do something like this
Expand|Select|Wrap|Line Numbers
  1. sw $t2,BaseC(0)
  2. lw $t1,BaseC(0)
  3. lw $t2,BaseA(0)
  4.  
that is exactly the same number of istructions needed to do XOR Swap
Expand|Select|Wrap|Line Numbers
  1. xor    $t1,$t1,$t2
  2. xor    $t2,$t1,$t2
  3. xor    $t1,$t1,$t2
  4.  
so I don't think XOR swap is so optimizing
Nov 29 '07 #8
Let's stick to C++ in this forum. MIPS/DLX is beyond me.
Nov 29 '07 #9
I'm sorry,you're right;let's stick to high level!
Nov 29 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

38 posts views Thread by JKop | last post: by
28 posts views Thread by OSHIMA | last post: by
21 posts views Thread by Abhishek Jha | last post: by
14 posts views Thread by pras.vaidya | last post: by
7 posts views Thread by sachsase | last post: by
25 posts views Thread by indrawati.yahya | last post: by
10 posts views Thread by Chad | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.