void swap(int &x, int &y)
{
x -= y;
y += x; // y gets the original value of x
x = (y - x); // x gets the original value of y
}
Enjoy testing it out!
swapping two variables without using a temp var
#1
Posted 31 July 2003 - 03:59 PM
#2
Posted 01 August 2003 - 06:22 AM
the code below, have the exact same behaviour.
void swap2(int &x, int &y)
{
x ^= y ^= x ^= y;
}
#3
Posted 03 August 2003 - 08:13 PM
haven't seen the +- above yet..
but fastest is still with a temp variable.. register actually..
__asm {
mov eax,a
mov ebx,b
mov b,eax
mov a,ebx
}
or possibly there is even some way with mmx? :D
similar there is something for the fu**ing fpu..
but its always fun to swap "mathematically".. without tempvar..
with that idea, people where able to define the radix sort, the only sort faster than the minimum O(n*lg2(n)).. hehe:D
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#4
Posted 04 August 2003 - 06:14 AM
_asm
{
xchg [a], eax
xchg [b], eax
xchg [a], eax
}
but if you're resorting to assembly swaping of variables you're probably
already writing the loop in assembly and trying to hold everything in
registers.
#5
Posted 04 August 2003 - 06:17 AM
any other swap-codes?
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#6
Posted 04 August 2003 - 08:22 AM
push [a] push [b] pop [a] pop [b]
not very effective and even worse for register only swaps where xchg should
be used, but it's diffrent...
for floats you could use:
fldp [a] fldp [b] fxch fstp [a] fstp [b]
using mmx you could use the pshufw instruction family.
#7
Posted 22 September 2003 - 08:58 AM
davepermen said:
void swap(int& a, int&b )
{
int c = a;
a = b;
b = c;
}
sorry. couldnt resist.
- Me blog
#8
Posted 20 October 2003 - 09:31 AM
void swap(int *x,int *y)
{
int second_varible;
int third_variable;
// See?! I'm not using the second!!!
third_variable = *y;
*y = *x;
*x = third_variable;
}
#9
Posted 20 October 2003 - 11:13 AM
no wait you are serious about your post ?
#10
Posted 20 October 2003 - 01:42 PM
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#11
Posted 21 October 2003 - 01:22 AM
#12
Posted 21 October 2003 - 09:24 AM
#13
Posted 21 October 2003 - 11:11 AM
#14
Posted 13 October 2004 - 09:09 PM
Mine is one line of code though ;-)
inline void
UselessSwap(int& a, int &b)
{
a -= b += a -= b = -b;
}
#15
Posted 10 October 2006 - 11:29 PM
#16
Posted 11 October 2006 - 01:19 AM
What a great thread.. I like the last one.. Never thought about this solution (did anyone tried if it really works?). I also like the function name as it really sais what it's all about.
These "optimizations" are nowadays slower than a temporary variable. But they re cool nevertheless.
I like bit-tricks like these alot.
#17
Posted 11 October 2006 - 09:26 AM
#18
Posted 11 October 2006 - 11:20 PM
FarooqMela said:
Mine is one line of code though ;-)
inline void
UselessSwap(int& a, int &;)
{
a -= b += a -= b = -b;
}
Not that I'm whining or anything, but if my understanding is correct about certain aspects of c++, the above code is going straight for the realm of "undefined behavior" in c++.
Someone correct me if I'm wrong.
- Me blog
#19
Posted 11 October 2006 - 11:26 PM
-
Currently working on: the 3D engine for Tomb Raider.
#20
Posted 11 October 2006 - 11:39 PM
The reason you're wrong is because the associativity of these expressions is well defined. Just as a + b + c + d is parsed as ((a +
Each = operator returns a reference to *this and is used in the outer expression. The thing that isn't defined is the order of evaluation (which is different from associativity) in the function argument list. ++a = a++ is undefined because that translates to operator=(++a, a++) and the outcome depends on which is evaluated first: ++a or a++.
-
Currently working on: the 3D engine for Tomb Raider.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users












