01-19-2004, 06:04 PM | #1 (permalink) | |
Addict
Location: Ottawa, ON, Canada
|
[C] XOR Challenge
Free cookie to the first person who can figure out what this little piece of C code is trying to accomplish:
Quote:
Warning: Submitter does not have any actual cookies to give out. Respondants will have to suffice with Submitter's admiration and respect. Void where prohibited. Edit: This has already been answered below by HFrankenstein. Don't read any further if you want to do this challenge by yourself.
__________________
"A witty saying proves nothing" - Voltaire Last edited by Quadraton; 01-19-2004 at 09:22 PM.. |
|
01-19-2004, 07:57 PM | #2 (permalink) |
Crazy
Location: New Jersey / Delaware
|
Here goes...
To make it easier to look at, stick some parens in there (though I believe it will produce a syntax error) [edit]VC++ doesn't seem to mind.[/edit] x ^= (y ^= (x ^= y)) The innermost statement (x ^= y) will set x = 100(bin) = 4(dec) Then, the middlemost(?) statement (y ^= (x ^= y)) sets y = (101 ^ 100) = 1 Finally, the outermost statement x ^= (y ^= (x ^= y)) sets x = (100 ^ 001) = 101(bin) = 5(dec) In effect, it switches x and y. I could show the algebra that proves that this works for any x and y, but I'm lazy, so bleh.
__________________
When in doubt, sauerkraut. |
01-19-2004, 09:11 PM | #3 (permalink) |
Addict
Location: Ottawa, ON, Canada
|
HFrankenstein, you have my admiration and respect, especially for such a detailed answer.
Yes indeed, it is a swapping algorithm that doesn't require a tertiary (temporary) variable. I believe it only works on integers (i.e. not floating points), and it should work for any combination of numbers (both positive and negative). As an additional benefit, the compiler can translate it into 3 simple XOR assembler statements, making it much more efficient to execute.
__________________
"A witty saying proves nothing" - Voltaire Last edited by Quadraton; 01-20-2004 at 11:27 AM.. |
01-21-2004, 03:00 PM | #5 (permalink) | |
Location: Waterloo, Ontario
|
Quote:
|
|
02-22-2004, 07:15 PM | #12 (permalink) |
Crazy
Location: The state of denial
|
I remember this peice of code... funny thing is this operation uses more lines of assembly code (4 instead of 3 if i remember correctly) and the same number of registers than a simple snippet like this:
int x = 5; int y = 1; int temp; temp = x; x = y; y = temp; So it is less efficient even though you can write it on one line. The code using XOR is also much harder for people to glance at and understand. So remember kids, writing slick code like this isn't necessarily better.
__________________
Smoke me a kipper, I'll be back for breakfast. |
02-24-2004, 05:03 PM | #13 (permalink) |
Junkie
Location: San Francisco
|
Yeah it looks like it's a couple of cycles slower per swap. Unless there is some crazy x86 instruction I don't know about to do it better.
Code:
mov eax, x mov ebx, y xor eax, ebx xor ebx, eax xor eax, ebx mov x, eax mov y, ebx Code:
mov eax, x mov ebx, y mov y, eax mov x, ebx Last edited by n0nsensical; 02-24-2004 at 05:06 PM.. |
02-24-2004, 06:27 PM | #14 (permalink) | |||
I am Winter Born
Location: Alexandria, VA
|
Quote:
Also, as an amusing note, here's a funny bit: Quote:
Quote:
|
|||
Tags |
challenge, xor |
|
|