Please don't use this in an interview
Ok, I've allowed myself to be drawn into this conversation for the last time. Please, please, don't use this sort of question in an interview. You want to hire motivated, creative individuals. Whether they know this sort of trivia is, IMHO, irrelevant. I'd much rather hire someone with some fire in his belly than some know-it-all who's going to win the next round of Jeopardy. Sure, ask him/her what xor/xor/xor will do, and observe his/her reaction. If it's a blank stare, deduct points. If it's anything else, add points. Again, JMHO. Sorry!
Re: Please don't use this in an interview
Ok, the suggestion will be considered. Thanks.
But, I really don't intend to use it as an interview question,
I prefer including it,I bet you won't disagree, in a written exam.
Here's another version !The only one that is to spec.!
As MS compilers allow inline assembly I thought it proper to include an example,
it's actually more within spec than any other solution that's been posted as it
truly only uses two variables and only two cpu registers.
About using it for interviews, IMHO YES, if the person is going to expected to
think and use the tools that are available. Would you use a sledg hammer to crack
nuts when a nutcracker is available ?
regards
Hans Wedemeyer
#include <stdlib.h>
void main( int argc, char* argv[] )
{
int a = 7;
int b = 9;
// did user supply new values ?
if ( argc > 1 && argc == 3 ) //expect two args a followed by b
{
a = atoi(argv[1]);
b = atoi(argv[2]);
}
printf("a=%d b=%d\n",a,b);
_asm {
mov eax,a
mov ebx,b
xchg eax,ebx
mov a,eax
mov b,ebx
}
printf("a=%d b=%d\n",a,b); // within spec. but limited to 2gig
}