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