Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Calculating Hamming distance (binary)

by jwkrahn (Monsignor)
on Aug 11, 2013 at 07:01 UTC ( #1048981=note: print w/ replies, xml ) Need Help??


in reply to Calculating Hamming distance (binary)

#Hamming distance sub hd { my ($d1,$d2,$bits) = @_; $bits ||= 8; my $diff = $d1 ^ $d2; my $count = 0; for my $b ( 0 .. $bits-1 ) { my $mask = 1 << $b; ++ $count if $diff & $mask } return $count }

When I run your subroutine I get the warning message:

Argument "^C" isn't numeric in bitwise and (&) at -e line XX.

That message means that $diff is a string and $mask is a number.    You can use the bit-wise operators like & on two strings or two numbers but not on a number and a string.

perl provides a way to count bits in a string using the unpack function.


Comment on Re: Calculating Hamming distance (binary)
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-12-17 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (31 votes), past polls