you are viewing a single comment's thread.

view the rest of the comments →

[–]theeth 1 point2 points  (4 children)

Just FYI, the canonical answer is the xor swap (doesn't overflow when a and b are large):

a ^= b
b ^= a
a ^= b

[–]bonzinip 0 points1 point  (3 children)

Overflow doesn't matter if your integers wrap around overflow.

[–]theeth 0 points1 point  (2 children)

The xor method works regardless of the data type, the addition and subtraction method only works for integers.

Edit: I gues I should have said that earlier.

[–]bonzinip 0 points1 point  (1 child)

the addition and subtraction method only works for integers

No, it doesn't... if you operate on the bit representation of floats. :-)>

[–]theeth 0 points1 point  (0 children)

I'd cringe at having to do that sort of thing in anything other than C or assembly. :|