1016 B
1016 B
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
- Return the SUBTRACTION of swapped numbers by XOR bitwise operations.
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.