2 ms·
It's not quite the same as XOR swap, but a trick I've found handy is conditionally swapping values using XOR: int a, b; bool cond; int swap =
by anematode 6mo ago
It's not quite the same as XOR swap, but a trick I've found handy is conditionally swapping values using XOR:
int a, b;
bool cond;
int swap = cond ? a ^ b : 0;
a ^= swap;
b ^= swap;
If cond is highly unpredictable this can work rather nicely.