Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

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.2 with 13 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 ..?

by daisy on Tuesday, March 16th 2010 at 08:57 AM

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,temp;
clrscr();
void swap(int,int)
printf("enter the two numbers");
scanf("%d%d",

by jaanu on Wednesday, March 17th 2010 at 09:15 AM

thank u ...........

by jaanu on Wednesday, March 17th 2010 at 09:15 AM

thank u ...........

by jaanu on Wednesday, March 17th 2010 at 09:15 AM

thank u ...........

by jaanu on Wednesday, March 17th 2010 at 09:15 AM

thank u ...........

by jaanu on Wednesday, March 17th 2010 at 09:15 AM

thank u ...........

by MUHAMMAD HUSSEIN on Monday, April 26th 2010 at 07:51 PM

I LOVE C PROGRAMMING.
PLS.SEND ME A SOFT COPY THROUGH MY EMAIL.
THANKS.

by MUHAMMAD HUSSEIN on Monday, April 26th 2010 at 07:52 PM

I LOVE C PROGRAMMING.
PLS.SEND ME A SOFT COPY OF C TEXT BOOK THROUGH MY EMAIL.
THANKS.

by MUHAMMAD HUSSEIN on Monday, April 26th 2010 at 07:52 PM

I LOVE C PROGRAMMING.
PLS.SEND ME A SOFT COPY OF C TEXT BOOK THROUGH MY EMAIL.
THANKS.

by daisy on Wednesday, April 28th 2010 at 10:13 AM

thanks that u like c programming..
by the what r u doing roght now???
i mean r u a student or any professional???
plz give ur complete email address and i will try for giving the soft copy to u.......
thanks for commenting..........
with regards daisy

by daisy on Wednesday, April 28th 2010 at 10:13 AM

thanks that u like c programming..
by the what r u doing roght now???
i mean r u a student or any professional???
plz give ur complete email address and i will try for giving the soft copy to u.......
thanks for commenting..........
with regards daisy

by daisy on Wednesday, April 28th 2010 at 10:17 AM

this is another method to swap two numbers without using a variable::
#include<stdio.>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("enter any two numbers");
scanf("%d%d",

by shank on Thursday, June 24th 2010 at 05:37 AM

thanks for the info(ruchi)

by Abi on Monday, July 19th 2010 at 10:54 AM

#include<iostream.h> #include<conio.h>
void swap(int

by sandy on Monday, July 19th 2010 at 01:00 PM

can we swap two numbers without use of any third variable and using only bitwise and(

by sandy on Monday, July 19th 2010 at 01:00 PM

can we swap two numbers without use of any third variable and using only bitwise and(

by sandy on Monday, July 19th 2010 at 01:00 PM

can we swap two numbers without use of any third variable and using only bitwise and(

by Abbas on Monday, July 19th 2010 at 05:00 PM

A=1 , B=0

One Way

B=A OR B
A=A AND B

another way

A= A AND B
B= NOT(A)

there might many more

by sandy on Tuesday, July 20th 2010 at 03:02 PM

to abbas

i dont think ur program is write when i take
A=2 B=3

i am not able to swap the numbers using bitwise and and bitwise or operator as said by you in your code

by sandy on Tuesday, July 20th 2010 at 03:02 PM

to abbas

i dont think ur program is write when i take
A=2 B=3

i am not able to swap the numbers using bitwise and and bitwise or operator as said by you in your code

by sandy on Tuesday, July 20th 2010 at 03:06 PM

for both the cases after swaping i am getting result as 3,3 when i have taken A=2,B=3

and for A=1,B=0 i am getting the result after swapping as 0,0

by Abbas on Wednesday, July 21st 2010 at 01:02 AM

oh Sandy Now Check

A=1 , B=0

One Way

B=A OR B
A=A NAND B

another way

A= A AND B
B= NOT(A)

there might many more

by sandy on Friday, July 23rd 2010 at 12:06 PM

to abbas

no this isn't working too please check the code you have posted. next time run it on any compiler and do upload if u get any successful swapping technique...

by sandy on Friday, July 23rd 2010 at 12:06 PM

to abbas

no this isn't working too please check the code you have posted. next time run it on any compiler and do upload if u get any successful swapping technique...

by shank on Friday, July 23rd 2010 at 08:59 PM

Sandy:


Assume two numbers a and b.


the logic here to be used is

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


eg: a=5,b=6
a=5 6 ie.a=11
b=11-6 ie.b=5
a=11-5 ie a=6


a=6 and b=5 thus they are swapped ma... take care all the best

by Abbas on Saturday, July 24th 2010 at 04:20 AM

Sandy
Dear that Swaping for BIT using AND Operator

by Ashwani sharma on Monday, July 26th 2010 at 03:37 AM

Please send me shorting programs in c
I have to need it.

by Sandy on Wednesday, July 28th 2010 at 09:11 AM

Hey shank

I am asking a pgm for swapping of two variables with out using third variable and also using bitwise and/or operators only. Not, using any other arithmetic operators

by mee on Friday, July 30th 2010 at 03:35 PM

dats really very useful.. thanks a lot..

by mee on Friday, July 30th 2010 at 03:35 PM

dats really very useful.. thanks a lot..

by mee on Friday, July 30th 2010 at 03:35 PM

dats really very useful.. thanks a lot..

by mee on Friday, July 30th 2010 at 03:35 PM

dats really very useful.. thanks a lot..

by Abi on Friday, July 30th 2010 at 04:22 PM

//Swapping of 2 variables without using 3rd variable

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a = ");
scanf("%d",

by prasanna on Monday, August 2nd 2010 at 06:41 AM

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("enter two numbers");
scanf("%d%d",

by lingaraj on Monday, August 2nd 2010 at 06:45 AM

prasanna ur program is very superb thanks for sent mail,do u hav any other pgm pls mail me

by shank on Monday, August 2nd 2010 at 01:00 PM

sandy,


i'll send it by tomo

by shank on Monday, August 2nd 2010 at 01:00 PM

sandy,


i'll send it by tomo

by shank on Monday, August 2nd 2010 at 01:00 PM

sandy,


i'll send it by tomo

by shank on Monday, August 2nd 2010 at 01:00 PM

sandy,


i'll send it by tomo

by shank on Monday, August 2nd 2010 at 01:00 PM

sandy,


i'll send it by tomo

by shank on Monday, August 2nd 2010 at 01:00 PM

sandy,


i'll send it by tomo

by shank on Monday, August 2nd 2010 at 01:00 PM

sandy,


i'll send it by tomo

by shank on Monday, August 2nd 2010 at 01:00 PM

sandy,


i'll send it by tomo

by shank on Monday, August 2nd 2010 at 01:00 PM

sandy,


i'll send it by tomo

by shank on Monday, August 2nd 2010 at 01:00 PM

sandy,


i'll send it by tomo

by shank on Monday, August 2nd 2010 at 01:00 PM

sandy,


i'll send it by tomo

by shank on Monday, August 2nd 2010 at 01:00 PM

sandy,


i'll send it by tomo

by MIkE on Monday, August 16th 2010 at 07:23 PM

A =B; B=A-B; A=B-A
is not working for high values, because they will go out of range!

A*=B; B=A/B; A=A/B
same problem as above and also is not working if one of the values is 0

A^=B; B^=A; A^=B (^= meaning xor ;)
can't see any problems with this!

you can get xor with and,or and not
e.g.: A xor B = (A not and B) and (A or B)

but normally the system already gives you xor, when you got and/or/not :)

by vikram on Tuesday, August 24th 2010 at 03:08 AM

a=((a b)-(b=(a-b) b))

by vikram on Tuesday, August 24th 2010 at 03:08 AM

a=((a b)-(b=(a-b) b))


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 >>
Sponsors
Discover Geekpedia

Other Resources