Click here to Skip to main content
15,868,340 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.5K   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 
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 
Yes, it still creates a temporary variable.
So you don't really save anything.
Assignments can be faster than math, so this neat trick could run slower without reducing memory usage.
Besides you can always make the temp value a register variable.

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.