in reply to Flying Obfu

I've seen this several dozen times and still have no clue why it works. Can anyone write up a nice explanation?

Replies are listed 'Best First'.
Re: Re: Flying Obfu
by jasonk (Parson) on Apr 09, 2003 at 21:47 UTC

    Essentially it does this:

    # let's assume some values for the sake of discussion $a = 'foo'; $b = 'bar'; $a = $a ^ $b; # $a now contains both variables, $a and $b logical or'ed # together, so at this point a=foo^bar and b=bar $b = $a ^ $b; # logically or-ing them again (since b still contains bar) # returns the original value of a, so now a=foo^bar # and b=foo $a = $a ^ $b; # then we logical-or the combined value with b, which now # contains our original a value. this returns the # original b value, so now a=bar and b=foo

    It's one of those things that you can look at a dozen times, and suddenly your brain just goes 'ah ha!'.

    Update: Of course read 'exclusive or (or xor)' everywhere that I said 'logical or', it must be bedtime.


    We're not surrounded, we're in a target-rich environment!
Flying Obfu Demystified
by Mr. Muskrat (Canon) on Apr 09, 2003 at 21:50 UTC
    $a = ord('A'); # 01000001 $b = ord('B'); # 01000010 $a = $a ^ $b; # 01000001 XOR 01000010 = 00000011 $b = $a ^ $b; # 00000011 XOR 01000010 = 01000001 $a = $a ^ $b; # 00000011 XOR 01000001 = 01000010
Re: Re: Flying Obfu
by dze27 (Pilgrim) on Apr 14, 2003 at 17:21 UTC

    Or, if you think in terms of truth tables:

    original $aoriginal $b$a ^ $b (gives new $a)$a ^ $b (gives new $b)$a ^ $b (gives new $a)
    00000
    01101
    10110
    11011

    As you can see, original $a is the same as new $b and original $b is the same as new $a.