Jump to content
  • Advertisement

Archived

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

farmersckn

Swap macro

This topic is 6493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Why not something like this:
    
template <class T> inline void swap(T & op1, T & op2)
{
T temp = op1;
op1 = op2;
op2 = temp;
}


Share this post


Link to post
Share on other sites
quote:

Why not something like this:



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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!