Swapping two integers with bitwise XOR

suggest change
var a = 11, b = 22;
a = a ^ b;
b = a ^ b;
a = a ^ b;
console.log("a = " + a + "; b = " + b); // a is now 22 and b is now 11

^ converts each operand with ToInt32, so this swaps 32-bit integers. It fails when both sides name the same binding, because a = a ^ a becomes 0. The expressions still produce ordinary numbers; they do not avoid allocation.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents