Click here to Skip to main content
15,887,895 members
Articles / Programming Languages / C#
Tip/Trick

Swaping values of two variable without temporary variable

Rate me:
Please Sign up or sign in to vote.
4.83/5 (18 votes)
11 Mar 2010CPOL 24.7K   20   9
I don't know whether these type of tips are allowed or not. But after having experience of core algorithm-base technical interview I thought I should share question I was asked in my interview.They asked me to show all methods I know to swap values of two variables.I Tried my level best...
I don't know whether these type of tips are allowed or not. But after having experience of core algorithm-base technical interview I thought I should share question I was asked in my interview.

They asked me to show all methods I know to swap values of two variables.

I Tried my level best to do it:
//1st method
a -= b;
b += a;         
a = (b - a);    

//2nd methos
a = b + a - (b = a)

//3rd method
a ^= b ^= a ^= b;

//4th method
a -= b += a -= b = -b;

//5th method
//Assuming a and b are none zero and int
a=a*b;
b=a/b;
a=a/b;

//6th method
//Assuming a and b are binary types
a %= b %= a %= b;


Note : All one line codes are compiler dependent only.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionmethod 2 is not working Pin
Sivaji156516-Mar-14 20:57
Sivaji156516-Mar-14 20:57 
GeneralFastest One :) Pin
Vitaly Tomilov28-Aug-12 12:50
Vitaly Tomilov28-Aug-12 12:50 
GeneralNice article, even in today's world it occurs rarely that on... Pin
nv319-Dec-11 22:26
nv319-Dec-11 22:26 
Generalgood for tiny processors with limited RAM Pin
Harold Bamford16-Mar-10 11:45
Harold Bamford16-Mar-10 11:45 
I remember having discussions about this kind of thing--in the 1970's with Z80 and 8080 processors. But I've never seen a legitimate reason for this on modern machines. Unless you have lots of ROM for code space and almost no RAM for variables, there is usually no good reason for doing things like this, IMHO.

In fact, I'll bet a temporary variable is created by the compiler for some of these! Depends on the target machine and the optimizer, of course.

Even now, where much of my coding is on an 8051 (don't pity me; I'm employed!), I never have to resort to such machinations.

However, it is good to know about such techniques for rounding out your software "Kung Fu." Smile | :) On that basis, I give your tip a 5.
GeneralRe: good for tiny processors with limited RAM Pin
chevu16-Mar-10 22:49
chevu16-Mar-10 22:49 
GeneralRe: good for tiny processors with limited RAM Pin
Pranit Kothari3-Jan-12 3:14
Pranit Kothari3-Jan-12 3:14 
GeneralRe: good for tiny processors with limited RAM Pin
huangyi520921-Mar-11 0:23
huangyi520921-Mar-11 0:23 
GeneralRe: good for tiny processors with limited RAM Pin
Pranit Kothari3-Jan-12 3:15
Pranit Kothari3-Jan-12 3:15 
GeneralRe: good for tiny processors with limited RAM Pin
JackDingler14-Mar-12 4:51
JackDingler14-Mar-12 4:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.