XOR Swap Algorithm - Reasons For Use in Practice

Reasons For Use in Practice

In most practical scenarios, the trivial swap algorithm using a temporary register is more efficient. Limited situations in which XOR swapping may be practical include:

  • On a processor where the instruction set encoding permits the XOR swap to be encoded in a smaller number of bytes;
  • In a region with high register pressure, it may allow the register allocator to avoid spilling a register.
  • In microcontrollers where available RAM is very limited.

Because these situations are rare, most optimizing compilers do not generate XOR swap code.

Read more about this topic:  XOR Swap Algorithm

Famous quotes containing the words reasons for, reasons and/or practice:

    “The more one analyses people, the more all reasons for analysis disappear. Sooner or later one comes to that dreadful universal thing called human nature.”
    —Oscar Wilde (1854–1900)

    “With no matter what human being, taken individually, I always find reasons for concluding that sorrow and misfortune do not suit him; either because he seems too mediocre for anything so great, or, on the contrary, too precious to be destroyed.”
    —Simone Weil (1909–1943)

    “My paternal grandmother would not light a fire on the Sabbath and piled all Sunday’s washing-up in a bucket, to be dealt with on Monday morning, because the Sabbath was a day of rest—a practice that made my paternal grandfather, the village atheist, as mad as fire. Nevertheless, he willed five quid to the minister, just to be on the safe side.”
    —Angela Carter (1940–1992)