Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 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:
int x = 1;
int y = 5;

x ^= y ^= x ^= y;
What are the contents of x and y after this piece of code has run?

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..
Quadraton is offline  
Old 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.
HFrankenstein is offline  
Old 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..
Quadraton is offline  
Old 01-19-2004, 10:16 PM   #4 (permalink)
Banned
 
Location: shittown, CA
cute
juanvaldes is offline  
Old 01-21-2004, 03:00 PM   #5 (permalink)
 
KnifeMissile's Avatar
 
Location: Waterloo, Ontario
Quote:
Originally posted by Quadraton
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.
Well, it "only works" on integers 'cause the compiler won't let you do anything else. This method can be used on anything that has a binary representation. On a computer, that's everything. C let's you do anything you want (it's popularity is probably largely due to this), so you can simply cast your floats and do your thing...
KnifeMissile is offline  
Old 01-21-2004, 04:18 PM   #6 (permalink)
Banned
 
Location: 'bout 2 feet from my iMac
entertainment was writing that up on the whiteboard in the lab and watching the freshman scratch their heads. thx for the day's amusement!!
cheerios is offline  
Old 01-29-2004, 08:50 PM   #7 (permalink)
Crazy
 
wow, that has to be the coolest piece of code I have seen in a long time, thanks.
WireX is offline  
Old 02-18-2004, 11:01 AM   #8 (permalink)
Crazy
 
Location: Raleigh, NC
Can someone explain to me how this works? I don't understand the ^= at all.

Thanks
Digilogic is offline  
Old 02-18-2004, 12:36 PM   #9 (permalink)
Crazy
 
Location: New Jersey / Delaware
"x ^= y" is the same as "x = x ^ y". ^ is XOR.
__________________
When in doubt, sauerkraut.
HFrankenstein is offline  
Old 02-18-2004, 05:45 PM   #10 (permalink)
Insane
 
does it return 0?
Corneo is offline  
Old 02-18-2004, 06:02 PM   #11 (permalink)
Crazy
 
Location: New Jersey / Delaware
See my original answer (second post). It switches x and y.
__________________
When in doubt, sauerkraut.
HFrankenstein is offline  
Old 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.
madcow is offline  
Old 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
vs.

Code:
mov eax, x
mov ebx, y
mov y, eax
mov x, ebx
But these are of course the human-written versions; the Visual Studio compiler generates 9 xors and movs for the first one and 6 for the second.

Last edited by n0nsensical; 02-24-2004 at 05:06 PM..
n0nsensical is offline  
Old 02-24-2004, 06:27 PM   #14 (permalink)
I am Winter Born
 
Pragma's Avatar
 
Location: Alexandria, VA
Quote:
Originally posted by madcow
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.
Well, yes and no. The xor code isn't necessarily for everyone, but when you get down to writing hardcore assembly for systems level programming, you'll use the XOR version.

Also, as an amusing note, here's a funny bit:
Quote:
C Code:
int x = 0;
Quote:
ASM Code:
xorl %eax,%eax
Why XOR instead of movl $0,%eax? Because the XOR version is faster.
Pragma is offline  
 

Tags
challenge, xor

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 05:36 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62