I read on a site that using xor swaps is fast because it doesn't use a temporary variable. Here's an example:
#include <stdio.h>
int main(void)
{
int a=234,b=789;
b=b^a;
a=b^a;
b=b^a;
printf("a=%d,b=%d",a,b);
return 0;
}
Why don't people use this technique in real life code? Is it just poor style? Is there something not well defined about it? Is it an optimisation that my compiler might produce from more clear code, automatically?
All answers are already there consider it just an addition-
->if both values goes for same memory address-result will be zero
->compilers can optimize away the temporary variable in the naive swap
->modern CPUs strive to execute instructions in parallel via instruction pipelines
but with XOR technique is considerably slower than using a temporary variable to do swapping because each operation depends on the result of previous
->x+Y may go for integer overflow
The performance gain is typically so small that the cost to "understandable code" is higher than the speed benefit obtained.