You can also use simple addition and subtraction method.
If a or b is too big, you get an integer overflow, which is "undefined behaviour" in pure C++ terminology. (But practically, it should work with almost all environments nowadays).
Also, both versions may cause havoc if a and b are of different integral types.
Well, I wouldn't say it's particularly usefull. A compiler would probably do that anyway.
It's just something that I picked up during my Assembly Language Programming course in University and the prof (who is like 1000 years old and still programs for MS DOS), was obsessed with efficiency. So we HAD to do things like this in our assignments or we'd lose marks.
so instead of writing
mov ax, 0
we HAD to write
xor ax, ax
or we'd lose marks.
So after 4 monthes of anal retentive obsession over this, quite a few of his optimization quirks have just stuck
I would NEVER write a ^= a in a higher level language code.
I actually did the XOR swap in a Java course once, and the marker had no idea why it worked lol
:P
heh.. yea, well.. reminds me.. Some processors have hard-coded zero registers so there is "nothing faster than setting something to 0" anyway. (But I'm not so sure about this. maybe that's something for another cocktail party)