XOR Swap Algorithm

Published by CppPythonDude in

This is more informational than a challenge. You can actually switch 2 variables with the XOR operation ^. XOR works with two arguments. It turns both arguments into their binary representations, and for each bit, returns 1 if they are different, 0 otherwise.

The return value is the decimal representation of the new binary string. So, if you don't know how to do it, go play around with it! After some time on paper, you will understand what is going on, and how it works.

Your job is to switch 2 variables using the XOR operator, which means your return statement should be return std::make_pair(a, b), but the variables need to be switched.

Examples

XOR(10, 41) ➞ (41, 10)

XOR(69, 420) ➞ (420, 69)

XOR(12345, 890412) ➞ (890412, 12345)

Notes

  • Remember to use std::make_pair() instead of just make_pair() (if you don't put using namespace std after #include <utility>).
  • If you're stuck, or don't have time to test out different cases, check the Resources tab.
Watch a quick demo on how Edabit works.