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
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
Sorry for my bad English
and also for my bad programming style...
ZaC'ZaCoder (?!)
Try searching the forum or internet for it. You know, with key words that are similar to your title...
Quzah.
Hope is the first step on the road to disappointment.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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.
Hope is the first step on the road to disappointment.
Give it time, we haven't seen the super-compressed (and even more broken) 1-liner XOR swap
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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?
Sorry for my bad English
and also for my bad programming style...
ZaC'ZaCoder (?!)
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);
Sorry for my bad English
and also for my bad programming style...
ZaC'ZaCoder (?!)
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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;
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.