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.
Thanks in advance
Printable View
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.
Thanks in advance
Try searching the forum or internet for it. You know, with key words that are similar to your title...
Quzah.
Here's one way to swap 2 integer variables a and b.
Code:a= a+b
b= a-b
a= a-b
Problem w/ the above code is that it might cause overflows.
Code:a = a^b;
b = a^b;
a = a^b;
> Problem w/ the above code is that it might cause overflows.
Problem with the above code, it's only guaranteed on unsigned ints.
Now that you know, treat it like a piece of trivia.
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.
Give it time, we haven't seen the super-compressed (and even more broken) 1-liner XOR swap :rolleyes:
You are right, it was very late and sleep made me to write without searching (I use it, don't you worry)
what a big trouble... if you are busy just don't care ;)
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?
I never knew poor English makes people forget things...
Because of my poor German I forgot to feed my dog.
Code:if ((enlgish == poor) && tired) printf("%s",few);
else printf("%s",more);
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:
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].Code:a = b + c + d;
--
Mats
You can do xor swap just fine in two lines:
And then of course there is this:Code:b ^= a ^= b;
a ^= b;
Code:b ^= a ^= b, a ^= b;