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
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
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
Please use temp variables, expressions like that make my brain start to boil
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
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
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
------------------------IUnknown *pUnkOuter"Try the best you cantry the best you canthe best you can is good enough" --Radiohead
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
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Why not something like this:
template <class T> inline void swap(T & op1, T & op2){ T temp = op1; op1 = op2; op2 = temp;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement