Jump to content


swapping two variables without using a temp var


33 replies to this topic

#1 Dia

    DevMaster Staff

  • Administrators
  • 1120 posts

Posted 31 July 2003 - 03:59 PM

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!

#2 DrunkenCoder

    Member

  • Members
  • PipPip
  • 97 posts

Posted 01 August 2003 - 06:22 AM

and don't try swapping a variable with itself...

the code below, have the exact same behaviour.
void swap2(int &x, int &y)
{
	x ^= y ^= x ^= y;
}


#3 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 03 August 2003 - 08:13 PM

hehe, the much beloved xor..

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
davepermen.net
-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 DrunkenCoder

    Member

  • Members
  • PipPip
  • 97 posts

Posted 04 August 2003 - 06:14 AM

smaller (but definatly not faster)

_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 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 04 August 2003 - 06:17 AM

i never got how xchg really works, but it looks similar to the xor-snippet above :D

any other swap-codes?
davepermen.net
-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 DrunkenCoder

    Member

  • Members
  • PipPip
  • 97 posts

Posted 04 August 2003 - 08:22 AM

well, the ones I can think of right now are
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 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 22 September 2003 - 08:58 AM

davepermen said:

any other swap-codes?
void swap(int& a, int&b )
{
  int c = a;
  a = b;
  b = c;
}

sorry. couldnt resist.

#8 Mihail121

    Senior Member

  • Members
  • PipPipPipPip
  • 1059 posts

Posted 20 October 2003 - 09:31 AM

Here is a swap performed without a second variable. My creation so hands off!!!!

 

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 anubis

    Senior Member

  • Members
  • PipPipPipPip
  • 2225 posts

Posted 20 October 2003 - 11:13 AM

:lol: :lol: :lol:

no wait you are serious about your post ?
If Prolog is the answer, what is the question ?

#10 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 20 October 2003 - 01:42 PM

hehe
davepermen.net
-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 Smokey97

    New Member

  • Members
  • PipPip
  • 12 posts

Posted 21 October 2003 - 01:22 AM

lol, i saw this post a few mins after he posted it... and i thought it must have been a joke, so i didnt say anything... but i guess ti isint a joke. :lol: :lol: :lol: :lol:
There's only one thing we're incapable of doing; thats being incapable of doing something.

#12 Mihail121

    Senior Member

  • Members
  • PipPipPipPip
  • 1059 posts

Posted 21 October 2003 - 09:24 AM

What's wrong with you people? Why do u laugh? Of course i'm serious. Don't u see it works perfecly fine?! I use this one a lot in my projects. Stable and fast and everything. Yep it's cool :D

#13 anubis

    Senior Member

  • Members
  • PipPipPipPip
  • 2225 posts

Posted 21 October 2003 - 11:11 AM

hospital or police ???
If Prolog is the answer, what is the question ?

#14 FarooqMela

    New Member

  • Members
  • Pip
  • 3 posts

Posted 13 October 2004 - 09:09 PM

I had something similar sitting around in a header file...
Mine is one line of code though ;-)

inline void
UselessSwap(int& a, int &b)
{
  a -= b += a -= b = -b;
}


#15 momotaro

    New Member

  • Members
  • Pip
  • 1 posts

Posted 10 October 2006 - 11:29 PM

where the hell did u find this one! it is pretty cool!

#16 Nils Pipenbrinck

    Senior Member

  • Members
  • PipPipPipPip
  • 597 posts

Posted 11 October 2006 - 01:19 AM

Haha..

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 roel

    Senior Member

  • Members
  • PipPipPipPip
  • 698 posts

Posted 11 October 2006 - 09:26 AM

Funny thread indeed, resurrecting old threads isn't always that bad :)

#18 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 11 October 2006 - 11:20 PM

FarooqMela said:

I had something similar sitting around in a header file...
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.

#19 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 11 October 2006 - 11:26 PM

You're wrong ;)
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#20 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 11 October 2006 - 11:39 PM

Sorry, I was being a bitch :happy:
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 + ;) + c) + d and std::cout << "hello" << 34 << std::endl is equivalent to ((std::cout << "hello") << 34) << std::endl, this expression becomes a -= (b += (a -= (b = -B)))

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++.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users