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?

  1. because readability preferred on performance.

  2. because tmp = a; = b; b = tmp; not that slow.

  3. because compiler optimize anyway.

  4. because works integers only. if want swap floating-point numbers? strings? custom objects? etc.


Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -