
-----------------------------------
bugzpodder
Sun Jan 18, 2004 11:23 am

technicality in weekly quiz
-----------------------------------
just like to note that we cannot just swap any variables, they MUST be integers (or short int, long, long long in C++).  and they can be done in one statement in C++.  and i dont think Turing  even have such a command...

-----------------------------------
McKenzie
Sun Jan 18, 2004 11:38 am


-----------------------------------
(or char in C/C++) But the question never claimed to swap the value in any type of variable. As far as a built-in swap command, doing something in one line does not always translate into run-time efficiency when you go to the the number of machine instructions. 
All that said the question is more trivia than anything else but it is trivia that I've seen on a few "ultimate geek" sorta quizes. 
The real reason I submitted it is a bit of an inside joke however. :lol:

-----------------------------------
bugzpodder
Sun Jan 18, 2004 12:52 pm


-----------------------------------
actually i wasnt refering to the built-in swap command.  
we could combine the three lines as mentioned in the quiz to one statement, namely:
x^=y^=x^=y;
i think that would swap x and y.

to those who have never seen it before
that above line is equilvalent to
x^=y;
y^=x;
x^=y;

ie if we let our x=A y=B to begin with, after the first line,
x=A^B, y=B
after the second line
x=A^B, y=B^(A^B)=A^(B^B)=A
and after the third line
x=(A^B)^A=(A^A)^B=B
now you guys just have to figure out what the heck the ^ do...  its obviously not whatdotcolor if anything

-----------------------------------
McKenzie
Sun Jan 18, 2004 2:07 pm


-----------------------------------
But, all of C/C++ neato one-line shortcuts end up being expanded to multiple lines of machine code by the compiler making it the exact same. In this case 3 of course. The only real difference is the target audience. For some readers the 3 line version is a stupid waste of space, whereas for some the one line version involves  :think:

-----------------------------------
bugzpodder
Sun Jan 18, 2004 6:07 pm


-----------------------------------
haha, of course...
 :dance:

-----------------------------------
Andy
Sun Jan 18, 2004 6:10 pm


-----------------------------------
in c++, you can do everything in one line...

-----------------------------------
bugzpodder
Sun Jan 18, 2004 7:01 pm


-----------------------------------
cept for whatdotcolor.

-----------------------------------
Andy
Sun Jan 18, 2004 7:14 pm


-----------------------------------
sure u can
