I found out a neat trick to swap the values of two variables without having to create an auxiliar variable. I'll use javascript to illustrate it, although I'm guessing it's language agnostic.
let a = 1;
let b = 2;
a^=b; b^=a; a^=b;
console.log(a); // Prints 2
console.log(b); // Prints 1
Can someone explain how does the swap happen? What does ^=
do?