swapping two numbers

abhisek dash's Avatar, Join Date: Sep 2009
Newbie Member
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!!!!!!

if anyone does,please post it!
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
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: C++
a = a^b;
b = b^a;
a = a^b;
Or in a single line :
Code: C++
a^=b^=a^=b;

But this is **NOT** the best way, because suppose you define this :
Code: C++
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: C++
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: C++
#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 by SaswatPadhi; 30Sep2009 at 15:31.. Reason: Added lots of things :p
abhisek dash like this
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Quote:
Originally Posted by SaswatPadhi View Post
Or in a single line :
Code: C++
a^=b^=a^=b;
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.
abhisek dash like this
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
Quote:
Originally Posted by xpi0t0s View Post
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.
Yeah, I know abt that.
That's why I added :
Quote:
I know what you are trying to ask, and I am answering what you want to hear ::
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.
abhisek dash like this
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
abhisek dash like this