# the difference among these ways of swapping two ints

Discussion in 'C++' started by storyGerald@gmail.com, Nov 19, 2005.

1. ### Guest

I knew some ways of swapping two ints without using a temporary
variable. Just like:

// Method 1:
void swap1(int &a, int &b) {
int temp = a;
a = b;
b = a;
}

// Method 2:
void swap2(int &a, int &b) {
a = a + b;
b = a - b;
a = a - b;
}

// Method 3:
void swap(int &a, int &b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}

But I don't know the difference among these ways. The third method is
read from a book, and the author said that would be some cases where
this puzzle?

Thank you!

Gerald

, Nov 19, 2005

2. ### technatorGuest

Method 1 uses a temprorary variable. Method 2 is fairly simple.

Method 3 uses bitwise xor operator.

If initially a is 2 and b is 3.

a - 0010
b - 0011
------------
a - 0001
------------

a - 0001
b - 0011
------------
b - 0010
------------

a - 0001
b - 0010
------------
a - 0011
------------

So in the end a = 3 and b is 2.

Regards
Technator

technator, Nov 19, 2005

3. ### peter kochGuest

skrev:

> I knew some ways of swapping two ints without using a temporary
> variable. Just like:
>
> // Method 1:
> void swap1(int &a, int &b) {
> int temp = a;
> a = b;
> b = a;
> }
>
> // Method 2:
> void swap2(int &a, int &b) {
> a = a + b;
> b = a - b;
> a = a - b;
> }
>
> // Method 3:
> void swap(int &a, int &b) {
> a = a ^ b;
> b = a ^ b;
> a = a ^ b;
> }
>
> But I don't know the difference among these ways. The third method is
> read from a book, and the author said that would be some cases where
> this puzzle?

The first method works and could easily be modified to swap other types
of values such as strings.
The other two methods are at best non-portable (integer overflow is
undefined behaviour) and fail in swap(a,a);

/Peter
>
> Thank you!
>
> Gerald

peter koch, Nov 19, 2005
4. ### Fraser RossGuest

Method 3 I found is not faster than 1. That would be the main reason
for using it I expect but tests show it is slower. Method 2 would
probably be similar in speed to 3.

I don't see where any integer overflow is with method 3.

Fraser.

Fraser Ross, Nov 19, 2005
5. ### technatorGuest

Hello Pete

Sorry for sounding naive, but could u please elaborate?

Regards
Techantor

technator, Nov 19, 2005
6. ### =?ISO-8859-1?Q?Erik_Wikstr=F6m?=Guest

On 2005-11-19 14:37, technator wrote:
> Hello Pete
>
> Sorry for sounding naive, but could u please elaborate?
>
> Regards
> Techantor

An example would be if in the second method a+b is greater than the
largest number you can store in an integer. Since it is unspecified
behaviour you never know what will happen, will depend on architecture
and compiler.

Erik WikstrĂ¶m
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Nov 19, 2005
7. ### peter kochGuest

technator skrev:

> Hello Pete
>
> Sorry for sounding naive, but could u please elaborate?
>
> Regards
> Techanto

I'm not sure I understand, but you could try to test the algorithms
with
int i = ...; swap(i,i);

/Peter

peter koch, Nov 20, 2005
8. ### technatorGuest

Thanks Erik/ Pete.

I understand.

~Technator

technator, Nov 20, 2005
9. ### Jim LangstonGuest

<> wrote in message
news:...
>I knew some ways of swapping two ints without using a temporary
> variable. Just like:
>
> // Method 1:
> void swap1(int &a, int &b) {
> int temp = a;
> a = b;
> b = a;
> }

I believe this is a mistake. I think you actually meant:
void swap1(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}

because:
a = b;
b = a;

makes both those variables the same no matter what came before them. Think
about it. Say a = 5, b = 10.
a = b; a is now 10, b is still 10.
b = a; b is still 10, a is still 10.

Jim Langston, Nov 20, 2005
10. ### Karl Heinz BucheggerGuest

Fraser Ross wrote:
>
> Method 3 I found is not faster than 1. That would be the main reason
> for using it I expect but tests show it is slower. Method 2 would
> probably be similar in speed to 3.
>
> I don't see where any integer overflow is with method 3.
>

There aren't any.
It is Method 2 that suffers from overflow.
The problem with Method 3 is that you can't call it
with the same argument for a *and* b, as in swap( i, i );

The moral of the story:
While every week all those methods (and probably more) are rediscovered,
nothing beats the plain and simple 'use a temporary variable for swapping'
method. It is simple, elegant and works in all cases.

--
Karl Heinz Buchegger

Karl Heinz Buchegger, Nov 21, 2005