Geekpedia Programming Tutorials






How to swap two numbers without using a third variable

On Sunday, October 7th 2007 at 03:04 PM
By Andrew Pociu (View Profile)
*****   (Rated 4.6 with 8 votes)
Advertisement
More C++ Resources

Using pointers you can easily swap two variables without using an additional variable to store a temporary value. This is because instead of swapping values, you can swap addresses, as shown in this function:

void SwapSmart(int * a, int * b)
{
   *a = *a xor *b;
   *b = *b xor *a;
   *a = *a xor *b;
}

Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this Knowledge Base article
Comment Current Comments
by Ahmed Youssef on Tuesday, October 23rd 2007 at 02:47 AM

You may do something like this

void swap(int *a, int *b){
*a=*a+*b;
*b=*a-*b;

*a=*a-*b;
}

by sujith kumar on Monday, December 10th 2007 at 06:41 AM

Really Good

by Matt Lewis on Wednesday, January 16th 2008 at 09:35 PM

That would be what is known as the infamous Butterfly Switch.

by Dee on Friday, February 1st 2008 at 09:20 AM

ISO-C++ provides a swap function, so you don\'t need to write one yourself at all.

by A. Chakradhar Patro on Monday, February 4th 2008 at 01:36 AM

It is possible two swap 2 numbers without using arithmetic and bitwise operators?
If so how. Pl. send the code.

by A. Chakradhar Patro on Monday, February 4th 2008 at 01:39 AM

It is possible two swap 2 numbers without using arithmetic and bitwise operators?
If so how. Pl. send the code.

by Anand on Friday, February 15th 2008 at 05:58 AM

query..
how to swap to 2 numbers in one single text box using php program

by ruchita on Saturday, March 1st 2008 at 11:02 AM

its very simple


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

by ruchita on Saturday, March 1st 2008 at 11:03 AM

its very simple

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

by vidya on Wednesday, March 5th 2008 at 04:56 AM

a = a ^ b
b= a ^ b
a = a^ b

by karan on Sunday, April 27th 2008 at 05:12 AM

thanks ruchita................!

by Fadi Awan on Monday, November 10th 2008 at 12:11 PM

Guys u're fantastic..........

by Fadi Awan on Monday, November 10th 2008 at 12:11 PM

Guys u're fantastic..........

by Joe on Thursday, November 20th 2008 at 06:38 AM

Thanks dude! u r fantastic

by abhishek on Friday, December 12th 2008 at 03:08 AM

you all are dangerous

by Jeyaprakash on Saturday, January 10th 2009 at 06:53 AM

Is this program suitable for values a=65534 and b=65533

by Viswa on Thursday, March 12th 2009 at 04:03 AM

Thank u

by 3mie on Thursday, March 12th 2009 at 02:23 PM


How about for character???

by Chiz on Tuesday, March 24th 2009 at 06:24 AM

How it will be looks like in C#?

by Purna Magum on Tuesday, April 7th 2009 at 05:25 AM

a=a*b;
b=a/b;
a=a/b;

by kshama porwal on Tuesday, September 8th 2009 at 02:15 PM

hi purna ,ur answer is not giving right value when the value of variable is 0.

by subhash on Sunday, September 20th 2009 at 12:04 PM

thik u........sol my problem.

by srikanth on Thursday, October 8th 2009 at 02:44 AM

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleFunSwap
{
class Program
{
static void swap(int x,int y)
{
int c;
c = x;
x = y;
y = c;
}


static void Main(string[] args)
{
int a, b;
a = 300;
b = 200;
swap(a, b);
System.Console.WriteLine("{0}{1}", a, b);
System.Console.ReadLine();
}
}
}
"Here in values of a,b are swapped in function,
but when it come's to main again it's retaining its original values and i want the swapped values to be printed on console". if any one can help me out please.

by mani on Thursday, November 12th 2009 at 09:20 AM

how to swap two numbers using arithmetic operator.i want whole program.

by Syed Abbas Shamim Rizvi on Friday, January 8th 2010 at 11:32 PM

A=10
B=5

A=A B;
A=10 5
A=15

B=A-B;
B=15-5
B=10;

A=A-b;
A=15-10;
A=5

A=A B;
B=A-B;
A=A-B;

by Urmi on Monday, January 18th 2010 at 12:56 AM

Hey! Can anyone tell me how one would swap strings in C/C without using a third variable and using pointers? i tried using memcpy() the results were not very satisfying.

by Toby Lortz on Thursday, February 4th 2010 at 04:42 PM

@srikanth
This should work, you need to use "ref".

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleFunSwap
{
class Program
{
static void swap(ref int x, ref int y)
{
int c;
c = x;
x = y;
y = c;
}


static void Main(string[] args)
{
int a, b;
a = 300;
b = 200;
swap(ref a, ref b);
System.Console.WriteLine("{0}{1}", a, b);
System.Console.ReadLine();
}
}
}

by varsha on Wednesday, February 24th 2010 at 01:07 AM

thanx a lot.........

by varsha on Wednesday, February 24th 2010 at 01:07 AM

thanx a lot.........

by jaanu on Saturday, March 6th 2010 at 09:10 PM

Hai .... plz tell me how to write a c program to swap 3 values using "class"..

by jaanu on Saturday, March 6th 2010 at 09:14 PM

How to write a c program using class to swap 3 values ..?


Comment Comment on this Knowledge Base article
Name: Email:
Message:
Knowledge Base Related Knowledge Base Articles
There are no related KB articles.

Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C++ Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons