Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
technicality in weekly quiz
Author Message
bugzpodder

Posted: Sun Jan 18, 2004 11:23 am   Post subject: 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

Posted: Sun Jan 18, 2004 11:38 am   Post subject: (No subject)

(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.
bugzpodder

Posted: Sun Jan 18, 2004 12:52 pm   Post subject: (No subject)

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

Posted: Sun Jan 18, 2004 2:07 pm   Post subject: (No subject)

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
bugzpodder

Posted: Sun Jan 18, 2004 6:07 pm   Post subject: (No subject)

haha, of course...
Andy

Posted: Sun Jan 18, 2004 6:10 pm   Post subject: (No subject)

in c++, you can do everything in one line...
bugzpodder

Posted: Sun Jan 18, 2004 7:01 pm   Post subject: (No subject)

cept for whatdotcolor.
Andy

Posted: Sun Jan 18, 2004 7:14 pm   Post subject: (No subject)

sure u can