Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

overlapping XOR swap RE: interchanging variables the tough way

by tye (Sage)
on Aug 31, 2000 at 23:20 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://30617]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 04:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found