the difference among these ways of swapping two ints

S

storyGerald

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 method can't be used. I don't why, Would you please help me solve
this puzzle?

Thank you!

Gerald
 
T

technator

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
 
P

peter koch

(e-mail address removed) 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 method can't be used. I don't why, Would you please help me solve
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
 
F

Fraser Ross

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.
 
T

technator

Hello Pete

Sorry for sounding naive, but could u please elaborate?

Regards
Techantor
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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
 
P

peter koch

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
 
J

Jim Langston

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.
 
K

Karl Heinz Buchegger

Fraser said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top