Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   swapping two numbers (http://www.go4expert.com/forums/swapping-two-numbers-t19604/)

 abhisek dash 29Sep2009 20:26

swapping two numbers

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!!!!!!

Re: swapping two numbers

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 ;)

 xpi0t0s 30Sep2009 15:33

Re: swapping two numbers

Quote:
 Originally Posted by SaswatPadhi (Post 58221) 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.

Re: swapping two numbers

Quote:
 Originally Posted by xpi0t0s (Post 58223) 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.
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.

 xpi0t0s 30Sep2009 17:12

Re: swapping two numbers

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.

 All times are GMT +5.5. The time now is 17:43.