Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

overlapping XOR swap RE: interchanging variables the tough way

by tye (Sage)
on Aug 31, 2000 at 23:20 UTC ( #30617=note: print w/replies, xml ) Need Help??


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")

Replies are listed 'Best First'.
RE: overlapping XOR swap RE: interchanging variables the tough way
by Adam (Vicar) on Aug 31, 2000 at 23:48 UTC
    Well yes Tye, you can't XOR something with it-self and expect anything other then 0. That would be illogical. But it is a good point about the Fisher-Yates Shuffle, if you are using XOR to swap to locations you must be careful that the locations aren't the same... which is probably why the original algorithm does the test.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://30617]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2019-05-23 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (146 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!