swap macro done strange

Started by
3 comments, last by madman_k 20 years, 6 months ago
i was browsing some code i somehow got (i don''t remember how, but i think it was a tutorial source) and i found this macro definition. #define SWAP(A,B) { (A) ^= (B); (B) ^= (A); (A) ^= (B); } I had never seen a swap implemented this way before and I was wondering if anyone here had. Also wondering if you think it will be more effective then a simple templated inline Swap function in terms of speed? Thanks
Advertisement
There was a topic addressing this several days ago. Search for it.

How appropriate. You fight like a cow.
I''ve seen it. If I recall correctly, I found it to be slower than the "standard" swap method. If you really want to know for sure, do your own tests.
The x86 CPU has a XCHG (exchange) instruction.
Do not to try to be too clever.

And C++ does have a standard std::swap() function.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
thanks. Also, i found some other odd functions there:

#define BIG_NUM ((double)1.5 * (double)(1 << 26) * (double)(1 << 26))

inline int
Round(double a)
{
a += BIG_NUM;
return *(int*)(&a);
}

inline int
Trunc(double a)
{
a += BIG_NUM - 0.5;
return *(int*)(&a);
}

why not for trunc just do:
inline int trunc(double a)
{
return (int) a;
}

and for round:
inline int round(double a)
{
return (int) (a + .5f);
}

do you think there''s a reason behind the way he did it above?

This topic is closed to new replies.

Advertisement