c - Why don't people use xor swaps? -
i read on site using xor swaps fast because doesn't use temporary variable. here's 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 technique in real life code? poor style? there not defined it? optimisation compiler might produce more clear code, automatically?
because readability preferred on performance.
because
tmp = a; = b; b = tmp;
not that slow.because compiler optimize anyway.
because works integers only. if want swap floating-point numbers? strings? custom objects? etc.
Comments
Post a Comment