http://www.perlmonks.org?node_id=30617


in reply to RE: interchanging variables the tough way
in thread interchanging variables the tough way

sub xorswap { $_[0] ^= $_[1] ^= $_[0] ^= $_[1]; } $str="XORing is FUN!"; print "($str)\n"; xorswap( substr($str,0,6), substr($str,-6) ); print "($str)\n"; xorswap( substr($str,0,6), substr($str,-6) ); print "($str)\n"; xorswap( substr($str,0,9), substr($str,-9) ); print "($str)\n"; xorswap( $str, $str ); print "($str)\n"; __END__ (XORing is FUN!) (s FUN! iXORing) (XORing is FUN!) (!u'R y:u;ng is) ( )

Note that the last case is the real reason I think you shouldn't do XOR swapping. After all, it would break Adam's Fisher-Yates Shuffle improvement. ;->

P.S. I really only posted because I hadn't seen anyone code xorswap the "proper" way. (:

        - tye (but my friends call me "Tye")