Languages
[Edit]
EN

JavaScript - XOR swap algorithm

7 points
Created by:
troya
692

In this short article, we would like to show how to make number variables swap using the XOR swap algorithm using JavaScript.

Hint: the motivation to use XOR swap is when we work on the divices that have limited operating memory - e.g. micro controllers.

Quick solution:

How does it work?

The XOR operation can be inverted - it means, we do not lose any information with that operation. By executing it 3 times we are able to change variable value places.

 

See also

Edit
  1. JavaScript - bitwise XOR operator

References

Edit
  1. XOR swap algorithm - Wikipedia

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join