Swap macro
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
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
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
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
#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
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...
how could that float version of swap possibly be worth it? six type conversions, and alot of pointer dereferences..
------------------------
IUnknown *pUnkOuter
"Just do your best and
don't worry"
--Morrissey
"Try the best you can
try the best you can
the best you can is good enough"
--Radiohead
"Are you looking for new ways
to do better than your worst"
--Nick Drake
------------------------
IUnknown *pUnkOuter
"Just do your best and
don't worry"
--Morrissey
"Try the best you can
try the best you can
the best you can is good enough"
--Radiohead
"Are you looking for new ways
to do better than your worst"
--Nick Drake
No one ever said that the macro was faster =P
Null and Void
"In this house we obey the laws of thermodynamics!" --Homer Simpson
Null and Void
"In this house we obey the laws of thermodynamics!" --Homer Simpson
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
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
Why not something like this:
template <class T> inline void swap(T & op1, T & op2){ T temp = op1; op1 = op2; op2 = temp;}
quote:
Why not something like this:
Because std::swap already does this, and it''s specialised for some things (e.g. std::vector)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement