## In-place swap

Write a function to swap a number in place without temporary variables.

My initial thoughts:
Before swap, ‘a’ points to number $a$ and ‘b’ points to number $b$.

1. ‘a’ points to ‘a’ – ‘b’, which is $a - b$.
2. ‘b’ points to ‘b’ + ‘a’, which is $b + (a - b) = a$.
3. ‘a’ points to ‘b’ – ‘a’, which is $a - (a - b) = b$.

My initial codes:

```void swap(int & a, int & b) {
a = a - b;
b = b + a;
a = b - a;
}

Solution:
void swap(int & a, int & b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}

^ is the exclusive-OR operator:

0^0=0
0^1=1
1^0=1
1^1=0

Hence, we have:

a=a^b: ‘a’ will save all the bits that $a$ differs from $b$: if the bit that ‘a’ and ‘b’ differ, it gets 1, otherwise 0.
b=a^b: ‘b’ will compare to the difference between $a$ and $b$: for the bit that ‘b’ and ‘a’ differ, it means $a$ have 1 at this position and the result of a^b will assign that bit to 1; for the bit that ‘b’ and ‘a’ agree, it means $a$ have 0 at this position and the result of a^b will assign that bit to 0.

a=a^b: Same logic.

__ATA.cmd.push(function() {
__ATA.initVideoSlot('atatags-370373-5b27ff6f29761', {
sectionId: '370373',
});
});

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-26942-5b27ff6f297a2',  {
collapseEmpty: 'before',
sectionId: '26942',
width: 300,
height: 250
});
});

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-114160-5b27ff6f297a5',  {
collapseEmpty: 'before',
sectionId: '114160',
width: 300,
height: 250
});
});