Swapping variables without introducing a third one

Swapping 2 variables without introducing a third one can be useful when the efficiency is critical that you want everything stay in registers. There are a few ways to do this.

Notice that this method still works even when a overflows during the first addition. Try it to see what I mean.

You can also do this using XOR operator like so.


Would also like to mention the right way to do it in Python for readability. No one cares about performance when they are writing in Python right?


If you like my post, please give a little help to share it!
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
  • https://exorank.com/resellers.php White Label SEO

    Awesome post! Keep up the great work! 🙂

  • https://affiliatelabz.com/special/?source=www.codeescapism.com AffiliateLabz

    Great content! Super high-quality! Keep it up! 🙂