# swapping two numbers

Discussion in 'C' started by abhisek dash, Sep 29, 2009.

1. ### abhisek dashNew Member

Joined:
Sep 27, 2009
Messages:
4
0
Trophy Points:
0
you should know, how to swap two numbers using a third variable!

you may also know how to swap two numbers without using a third variable!

but you may not know how to swap two numbers without using any of the arithimetic operators and a third variable!!!!!!

2. ### SaswatPadhi~ Б0ЯИ Τ0 С0δЭ ~

Joined:
May 5, 2009
Messages:
1,343
55
Trophy Points:
0
Occupation:
STUDENT !
Location:
Orissa, INDIA
Welcome to G4EF

I know what you are trying to ask, and I am answering what you want to hear ::
Here is how you can do it without using any arithmetic op.s and any temp. var. :
Code:
```a = a^b;
b = b^a;
a = a^b;
```
Or in a single line :
Code:
```a^=b^=a^=b;
```
But this is **NOT** the best way, because suppose you define this :
Code:
```void swap(int &x, int &y)
{    x^=y^=x^=y;    }```
This would work fine for calls like swap(A,B), where A = 2 and B = 3 say.
But for calls like swap(A,A) or swap(A,B) where B=A, it would lead to entire loss of data (variable's contents).
So, be careful while using it.

Again, that won't work for data-types like float, double, structs, classes etc ...
So, you should mention in your question that the target vars are of the type int.

A generic swap function can be :
Code:
```template <class TYPE>
void swamp(TYPE &var1, TYPE &var2) {
TYPE var_temp = var1;
var1 = var2;
var2 = var_temp;
}
```
That one uses a temp. var., 'coz there's no way out.

You can do it this way too :
Code:
```#include <algorithm>
. . .
. . .
. . .
swap(x,y);
. . .
. . .
. . .
```
This one doesn't seem to use any arithmetic op.s or temp var.s, but works with a temp var. in the background

Last edited: Sep 30, 2009
3. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
This code has undefined behavior, since it modifies the lvalue a twice without an intervening sequence point.

See http://en.wikipedia.org/wiki/XOR_swap_algorithm

The "Reasons for avoidance in practice" part is also worth a read.

4. ### SaswatPadhi~ Б0ЯИ Τ0 С0δЭ ~

Joined:
May 5, 2009
Messages:
1,343
55
Trophy Points:
0
Occupation:
STUDENT !
Location:
Orissa, INDIA
Yeah, I know abt that.
The guy wanted that XOR form.

But I still don't understand why it has "undefined" behavior.
I read the theories, but am unable to find an example.

5. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
Because both "a^=" parts modify a, and there is no "sequence point" between the two. You will see a definition of C++ sequence points in the linked Wiki article, which does not list ^= as containing an implied sequence point.

> I read the theories, but am unable to find an example.
An example is not necessary, only the language specification. The language does not define the behaviour, and that has no implications for the behaviour of any specific compiler; many compilers may implement it according to the common understanding, but that does not mean the behaviour is defined.