XOR swap algorithm
Appearance
Xor swap is a simple algorithm which swaps the values of two variables without using a buffer. This algorithm follows:
- XOR the value of variable-one with variable-two and store the value in variable-one
- XOR the value of variable-one with variable-two and store the value in variable-two
- XOR the value of variable-one with variable-two and store the value in variable-one
This algorithm varies from other swap algorithms only in that it doesn't need to store the original value of variable-one in a temporary buffer prior to overwriting it and doesn't need to restore this value in variable-two.