![]() |
|
swapping variables - Printable Version +- Southperry.net (https://www.southperry.net) +-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14) +--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58) +--- Thread: swapping variables (/showthread.php?tid=4546) Pages:
1
2
|
swapping variables - Orit - 2008-10-27 Cheat? Cheat on what rules? The terms of the problem/riddle are: swap two variables without using extra storage. The XOR algorithm solves the riddle. Where's the cheating? swapping variables - Nikkey - 2008-10-27 It's not cheating though, it's just practical maths. a += b b = a - b a -= b a *= b b = a / b a /= b a ^= b b ^= a a ^= b Hell, even taking the integral and the derivative works with this method, just way less efficient. swapping variables - GummyBear - 2008-10-27 Orit Wrote:What? You don't need to split anything. In C, you'd code something like: When I need to swap strings, I would just swap the address of the pointers instead of swapping the array itself. if *p points to the first string and *q points to the 2nd string, swap p and q around and they're effectively the same as swapping values irrespective of the size of the string. BTW, xor is exclusive OR, ie either this or that, but not both and not none. |