• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Swap macro

## Recommended Posts

farmersckn    122
I saw one that didn''t use a temp variable, it xored the variables or something, is anyone familiar with this? if you can remember how it was written, i''d appreciate it. thanks

##### Share on other sites
Null and Void    1088
int a = 10, b = 20;
a = a ^ (b = (b ^ (a = (a ^ b))));

Afterwards: a = 20, b = 10.

 Null and Void"In this house we obey the laws of thermodynamics!" --Homer Simpson

##### Share on other sites
goir    122
Please use temp variables, expressions like that make my brain start to boil

##### Share on other sites
davepermen    1047
because of the problem, that you couldnt use binary operators onto floats, you could use the macro like this:

#define SWAP(a,b) {(*((int*)&a))^=(*((int*)&b));(*((int*)&b))^=(*((int*)&a));(*((int*)&a))^=(*((int*)&b));}

working for floats, too (it just reads the floats like ints, thats all, but its working, and thats what we want)

we wanna play, not watch the pictures

##### Share on other sites
BitMaster    8651
quote:
Original post by goir

Please use temp variables, expressions like that make my brain start to boil

Agreed. And davepermen''s is even more of a brain boiler. Wasn''t there a xchange assembler command (swap two 32 bit values)? I''m sure I read about it somewhere and if we get away from macros like this...

##### Share on other sites
pUnkOuter    122
how could that float version of swap possibly be worth it? six type conversions, and alot of pointer dereferences..

------------------------
IUnknown *pUnkOuter

don't worry"
--Morrissey

"Try the best you can
try the best you can
the best you can is good enough"

"Are you looking for new ways
to do better than your worst"
--Nick Drake

##### Share on other sites
Null and Void    1088
No one ever said that the macro was faster =P

 Null and Void"In this house we obey the laws of thermodynamics!" --Homer Simpson

##### Share on other sites
davepermen    1047
it works, that was all what I wannted to show, but, yep, its stupid, and a inline like that is bether

inline void swap(int& x,int& y)
{
int buffer=x;
x=y;
y=buffer;
}

because you could even use it for big classes (just overload it)

we wanna play, not watch the pictures

##### Share on other sites
BitMaster    8651
Why not something like this:
  template inline void swap(T & op1, T & op2){ T temp = op1; op1 = op2; op2 = temp;}

##### Share on other sites
Wilka    122
quote:

Why not something like this:

Because std::swap already does this, and it''s specialised for some things (e.g. std::vector)

##### Share on other sites
Houdini    266
BTW, just thought I'd mention the performance of the two ways mentioned to swap variables:

void Func1(){	static int a = 1;	static int b = 2;		a^=b^=a^=b;}void Func2(){	static int a = 1;	static int b = 2;		int t = a;	a = b;	b = t;}

Here's the time it took to run each function 100,000,000 times:
Function 1: 1.868420 seconds
Function 2: 1.264502 seconds

Not only is using a temporary value slightly faster, but it's also much easier to read.

- Houdini

Edited by - Houdini on October 8, 2000 10:07:25 PM

##### Share on other sites
farmersckn    122
I have to say, I hadn''t expected so many replies so fast, thanks. And, it seems, I didn''t *need* them, because the method i was looking for works slower. I''d already thought of and written the template swap function, but i thought the macro would be faster. thanks for proving me wrong. otherwise, i would have lost that .6% speed increase...
farmersckn

##### Share on other sites
Qoy    127
quote:

I''d already thought of and written the template swap function, but i thought the macro would be faster.

If you''re saying you thought the macro that involved the temp would be faster (not the XOR one), that''s not true. If you make the template function inline, then it would be the same speed. Templates only require extra work at compile time to be used. That is, there is no run time performance hit. All the templates do is to compile an instance of the function for every type that your code uses it with.