Solution
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int a = 0x07FFFFFF;
int b = 3;
cout << "a: " << a << " b: " << b << endl;
#if 0 // Method one
a = a^b;
b = a^b;
a = a^b;
#else // Method two
a = a + b;
b = a - b;
a = a - b;
#endif
cout << "a: " << a << " b: " << b << endl;
return 0;
}
Question
Write a function to swap 2 numbers in place without temporary variables
If single line is required, possible solutions is as follows
(1) a ^= b ^= a ^= b
(2) a = (a + b) - ( b = a), this one works on GCC but not Visual C++
(3) a = ((a = a + b) - (b = a - b)) works on Visual C++