Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [C] XOR Challenge (https://thetfp.com/tfp/tilted-technology/42169-c-xor-challenge.html)

Quadraton 01-19-2004 06:04 PM

[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.

HFrankenstein 01-19-2004 07:57 PM

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.

Quadraton 01-19-2004 09:11 PM

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.

juanvaldes 01-19-2004 10:16 PM

cute :)

KnifeMissile 01-21-2004 03:00 PM

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

cheerios 01-21-2004 04:18 PM

entertainment was writing that up on the whiteboard in the lab and watching the freshman scratch their heads. :D ;) thx for the day's amusement!!

WireX 01-29-2004 08:50 PM

wow, that has to be the coolest piece of code I have seen in a long time, thanks.

Digilogic 02-18-2004 11:01 AM

Can someone explain to me how this works? I don't understand the ^= at all.

Thanks

HFrankenstein 02-18-2004 12:36 PM

"x ^= y" is the same as "x = x ^ y". ^ is XOR.

Corneo 02-18-2004 05:45 PM

does it return 0?

HFrankenstein 02-18-2004 06:02 PM

See my original answer (second post). It switches x and y.

madcow 02-22-2004 07:15 PM

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.

n0nsensical 02-24-2004 05:03 PM

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.

Pragma 02-24-2004 06:27 PM

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. :D


All times are GMT -8. The time now is 07:25 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76