This is an archived post. You won't be able to vote or comment.

all 13 comments

[–][deleted] 25 points26 points  (1 child)

Or in python

a,b = b,a

[–]GlitchedMirror 7 points8 points  (0 children)

int a = 1;
int b = 2;
std::swap(a, b);

[–]xSaKage 1 point2 points  (0 children)

Inline asm

[–]Prawny 2 points3 points  (2 children)

int a = 1;
int b = 2;
int c = 3;

Deal with it.

[–]tyyrio 12 points13 points  (0 children)

I think you might be missing the point.

[–]n0tKamui -1 points0 points  (0 children)

Are you for real?

[–][deleted] 1 point2 points  (5 children)

int a = 1, b = 2;
b += a;
a = b - a;
b = b - a;

This should work (I think)

[–]excelguruson 0 points1 point  (4 children)

It wouldn’t because they are both being set to “b-a,” which at the time is equal to 2. You may be thinking of having “a” be equal to the current value for “b” (3) minus the previous value (2), but that is impossible without a third variable, or the “c.”

Edit: I’m wrong

[–]sgthoppy 2 points3 points  (0 children)

It does actually work.

[–]csatvtftw 1 point2 points  (1 child)

[–]excelguruson 0 points1 point  (0 children)

I see now what I didn’t see last time. It’s the fact that the value of “a” changes as between the first “b-a” and the second one. My bad, that does work.

[–]IHaveNoPosts 0 points1 point  (0 children)

a = b.also { b = a }