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


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:42 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360