Re: Improved Swapping Method In sort.c

David Newall
2008-04-30T18:47:01Z

Soumyadip Das Mahapatra wrote:
> static void u32_swap(void *a, void *b)
> {
> *(u32 *)b ^= *(u32 *)a ^= *(u32 *)b ^= *(u32 *)a;
> }
>

That was posted to comp.lang.c 20 years ago or more. It's not something
that the compiler is likely to be able to optimize (unlike the normal
swap, using a temporary variable.)

Another cute, temp-free swap, from the same era, is: b += a -= b; a = b - a.