Thanks Doug. Here\'s the fix:
void swap(int& a, int& b) { if (&a == &b) // added this check to ensure the same address is not passed in
Your swap a and b are the same location. The XOR hack only works when they are different locations.
a
b
I think in C; here's a table:
&a != &b &a == &b *a *b *a *b -5 -5 -5 -5 *a ^= *b; 0 -5 0 0 *b ^= *a; 0 -5 0 0 *a ^= *b; -5 -5 0 0