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:

    “Adolescents, for all their self-involvement, are emerging from the self-centeredness of childhood. Their perception of other people has more depth. They are better equipped at appreciating others’ reasons for action, or the basis of others’ emotions. But this maturity functions in a piecemeal fashion. They show more understanding of their friends, but not of their teachers.”
    —Terri Apter (20th century)

    “In short, if there were external bodies, it is impossible we should ever come to know it; and if there were not, we might have the very same reasons to think there were that we have now.”
    —George Berkeley (1685–1753)

    “I am out of practice at living.
    You are as brave as a motorcycle.”
    —Anne Sexton (1928–1974)