So , we have the simple integer swap:
void swap(int *x, int *y)
{
int *z = *x;
*x = *y;
*y = *z;
}
I know I’m naive and get excited by simple stuff, but I’m still amazed that it took me so long to find this alternative:
void swap(int *x, int *y)
{
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
I was beginning to wonder when XOR will help me in life…
P.S: works well especially for obsessive-compulsive maniacs which think their life won’t be peaceful if they don’t choose the best time/space complexity ratio for all their code.
