100-days-of-rust/Week-07/Day-45_Subtract-The-Swapped-Bits-Without-Temp-Storage
2023-03-23 21:14:34 -04:00
..
README.md Add files via upload 2023-03-23 21:14:34 -04:00

Subtract the swapped bits without temp storage

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 in their binary representation, and for each bit, returns 1 if they are different, 0 otherwise.

Create a function to swap int variables X and Y without the use of an additional temporary storage variable using only XOR bitwise operations.

Output

Example

XOR(10, 41) ➞ 31
XOR(69, 420) ➞ 351
XOR(12345, 890412) ➞ 878067
XOR(2, 1) ➞ -1

Notes

  • The swap operation needs to be done without the use of an additional temporary storage variable, using only XOR bitwise operations.

Constraints

  • The input numbers X and Y are positive integers.
  • The input numbers X and Y are different from each other.