473,394 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 7787
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

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

Similar topics

50
by: Steve | last post by:
How do you rewrite the swap function without using a tmp variable in the swap function???? int main() { int x = 3; int y = 5; // Passing by reference
38
by: JKop | last post by:
union SignedChoice{ long with_sign; unsigned long without_sign; }; int main() { SignedChoice data;
28
by: OSHIMA | last post by:
Hi, I found the swap() function without a temporary variable. void swap(int *x, int *y){ *x ^= *y; *y ^= *x; *x ^= *y; } So, I wrote the next code that exchange the double.
21
by: Abhishek Jha | last post by:
hello all, this has been an interesting topic since long. you can swap two numbers (say a and b) using temp(as third variable). you can also swap without using third variable.likeThis was also...
14
by: pras.vaidya | last post by:
hi, please help me with this problem : - how to swap two addresses .For eg variable i is located at 2000 and j at 3000 . Now i call swap function . Result should be that i should be now having...
1
by: clidezzzZ | last post by:
hi i want to know how to swap 2 variables in vb without using a temporary variable. thank you for the answer ^_^ god bless!!!!!
7
by: sachsase | last post by:
//swap of 2 variables void main() { int a=27,b=46; a=1?a-(b=1?((a=1?a+b:0)-b):0):0; printf("a=%db=%d",a,b); } I got output for this problem as a=46;b=27 Same but using scanf to read...
25
by: indrawati.yahya | last post by:
OK, before you all stab me with your OT pitchfork, I just want to mention that this question is indeed related to c.l.c++. My question is, does the well-known method to swap two integers produce...
10
by: Chad | last post by:
I hope this question doesn't appear twice. I tried posting it once, however, I got a stupid 505 internal error on google. When this happens, the post will either post 30 minutes later or won't post...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.