@nuclearcat: If you don't like VS, we don't have to dislike it.. Leave this up to personal preference. Compatibility issues will only matter when the person doing the programming will care (it doesn't when the person doing the programming doesn't care, as in when learning generally how to program)
---
xor operation is a basic logic operation. I believe it is as portable as it can get (no platform specifics).
As for rahmu's method, buffer overflows don't affect its correctness one bit - when applied using unsigned addition and subtraction.
C++ code exhibit.
It gets fucked up if you use signed chars. I am sure you can do some type casting specific for the swapping.
#include <iostream>
using namespace std;
void main(){
unsigned char a = 230;
unisgned char b = 50;
a = a + b;
b = a - b;
a = a - b;
cout << (unsigned int)a << ", " << (unsigned int)b << endl;
}
I liked rahmu's method as it is mathematically based. However, there's a larger pattern here. Any operation can be used as long as it is reversible (given one of the arguments).
Specifically:
[a, b]
[f(a,b), b]
[f(a,b), g(f(a,b), b)] = [f(a,b), a]
[g(f(a,b), a), a] = [b, a]
Thinking about it, g and f have to be linear functions on the domain they're applied to (which can be thought of in a modular way, so that the domain wraps around itself). That means that subtraction and addition is the only solution. Incidentally, exclusive or is both subtraction and addition when it comes down to a single bit.