In this video we'll explore one of the most simple, clever and despised of all bit hacks, the famous XOR swap!
The objective is to swap the values in two variables. In this video we'll explore four methods for achieving this, one of which is the XOR swap itself. We'll also look at the most common method, which uses a temporary variable. We'll look at the XCHG instruction and another very interesting swap called the Arithmetic Swap.
If my explanation of why the XOR swap works does not make sense to you, don't worry, it doesn't make sense to me either. XOR swap is one of those peculiar little gems of logic that I find only makes sense for a brief instant every now and again. Then I forget how it works again, and am left to marvel at its novelty with the happy bliss of ignorance.