Swap with XOR
Mediumjava
Read two integers a and b and swap their values using only the XOR operator (a ^= b; b ^= a; a ^= b;), then print them.
Input: two integers separated by whitespace.
Output: one line: a=<a> b=<b> after the swap.
Example 1
Input
10 20
Output
a=20 b=10
- 0 <= a, b <= 1000000000
Hint 1
Apply the three XOR-assignments in order.
Hint 2
XOR swap works even when a and b are equal (both stay the same).
XOR has the property that x ^ y ^ y == x. The three-step sequence uses this to exchange the two values without a temporary variable. Equal inputs are unaffected.