473,390 Members | 1,159 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,390 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 7786
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.

Nov 28 '07 #2
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
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
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)
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
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

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
by: JKop | last post by:
union SignedChoice{ long with_sign; unsigned long without_sign; }; int main() { SignedChoice data;
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.
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...
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...
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!!!!!
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...
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...
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...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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...
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,...
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: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.