Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^4: Hamming Distance Between 2 Strings - Fast(est) Way?

by BrowserUk (Pope)
on Jul 08, 2011 at 19:46 UTC ( #913409=note: print w/replies, xml ) Need Help??

in reply to Re^3: Hamming Distance Between 2 Strings - Fast(est) Way?
in thread Hamming Distance Between 2 Strings - Fast(est) Way?

^ in this case is bitwise-OR.

When you bitwise-OR two strings, where the bytes in the two strings are the same, the byte in the resultant string will be chr(0); when they are different, the results will be non-chr(0). This allows you to count the number of differences in two strings very quickly:

$seq1 = 'ACGTACGTACGTACGT';; $seq2 = 'TCGATCGATCGATCGA';; print ( $seq1 ^ $seq2 );; print length( $seq1) - ( $seq1 ^ $seq2 ) =~ tr[\0][\0];; 8

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2020-11-26 21:49 GMT
Find Nodes?
    Voting Booth?

    No recent polls found