# swap of variables without tmp

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-12-2009
ZaC
swap of variables without tmp
Is it possible?
Some months ago, a programmer told me it is, but he didn't tell me how. I'm still thinking about it.
• 06-12-2009
quzah
Try searching the forum or internet for it. You know, with key words that are similar to your title...

Quzah.
• 06-12-2009
MK27
Quote:

Originally Posted by ZaC
Is it possible?

Yes. You need a phenomenal sense of balance tho*, so we try to encourage people to just go pedestrian and use the "tmp".

*because, logically, the variables must pass each other in mid air
• 06-12-2009
lazyme
Here's one way to swap 2 integer variables a and b.

Code:

```a= a+b b= a-b a= a-b```
• 06-13-2009
this
Problem w/ the above code is that it might cause overflows.

Code:

```a = a^b; b = a^b; a = a^b;```
• 06-13-2009
Salem
> Problem w/ the above code is that it might cause overflows.
Problem with the above code, it's only guaranteed on unsigned ints.
• 06-13-2009
whiteflags
Now that you know, treat it like a piece of trivia.
• 06-13-2009
quzah
Or they could have just searched, learned ALL of this, because it's been posted a hundred times here, and saved each one of us the trouble of typing it all yet again!

Quzah.
• 06-13-2009
Salem
Give it time, we haven't seen the super-compressed (and even more broken) 1-liner XOR swap :rolleyes:
• 06-13-2009
ZaC
Quote:

Originally Posted by quzah
Try searching the forum or internet for it. You know, with key words that are similar to your title...

Quzah.

You are right, it was very late and sleep made me to write without searching (I use it, don't you worry)

Quote:

Originally Posted by quzah
Or they could have just searched, learned ALL of this, because it's been posted a hundred times here, and saved each one of us the trouble of typing it all yet again!

Quzah.

what a big trouble... if you are busy just don't care ;)

Quote:

Originally Posted by Salem
Give it time, we haven't seen the super-compressed (and even more broken) 1-liner XOR swap :rolleyes:

it's what I'm looking for, I already know the lazyme method, but becouse of my poor english and of my tiredness I forgot to write you about it.
So is not good the 1-linear xor swap?
• 06-13-2009
cyberfish
I never knew poor English makes people forget things...

Because of my poor German I forgot to feed my dog.
• 06-13-2009
ZaC
Code:

```if ((enlgish == poor) && tired) printf("%s",few); else printf("%s",more);```
• 06-13-2009
matsp
Quote:

Originally Posted by ZaC
So is not good the 1-linear xor swap?

You are only allowed to change a variable once in a sequence point [1], and the one-liner xor swap does modify the same variable more than once (because of the way that it swaps x with y, it changes x twice). This is not forbidded to write in the compiler, it is what is called "undefined behaviour" - the compiler may treat do "what it feels like" in this sequence of operations - it may decide to break it up in ways you wouldn't expect. Undefined behaviour means "anything can happen": nuclear missiles launch, incorrect result, or even correct result. It's a bit like driving across a junction when the light is red in your direction. If you are lucky, nothing bad happpens. If you have a bit of luck, you have a small collision, and if you are really unlucky, there is a BIG lorry crossing in the other direction, and your car is crushed.

[1] Sequence point is normally a statement in C, but there are some exceptions. Basically, a sequence point is the point at which the preceding code is completed, so if you say:
Code:

`a = b + c + d;`
the semicolon indicates the sequence point. If you were to look at the place where a is stored somewhere WITHIN the code generated for that line, it may contain the old value before that line, some partial value, or the whole sum of b + c + d, depending on where you look at it [and it may contain some spurious intermediate results at some point too, such as c + d or b + c, or even b + d, depending on how the compiler composes the code].

--
Mats
• 06-13-2009
this
Quote:

Originally Posted by Salem
Give it time, we haven't seen the super-compressed (and even more broken) 1-liner XOR swap :rolleyes:

Code:

```b = (a ^= (b ^= a)) ^ b;  a ^= b ^= a ^= b; // O_O```
*read matsp post above for problems...
• 06-13-2009
King Mir
You can do xor swap just fine in two lines:
Code:

```b ^= a ^= b; a ^= b;```
And then of course there is this:
Code:

`b ^= a ^= b, a ^= b;`
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last