Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1048981]
help
Chatterbox?
[thao4]: thao4
[hippo]: Indeed you are.
[thao4]: Hi all, I am new in perl. I need do somthing in perl: exemple: - search a patern "CADRE 1.1 CPU" in one file.txt - incremente +30 lines from the line with the patern found - replace the patern "Model Regle" by "Heath Rule A" in this new line (30 line
[marto]: good morning all
[hippo]: Welcome. Tell me, have you read perlintro? That's a great place to start.
[thao4]: I seach to do that since a week but I can't found the answer in google. Thank for your help
[Eily]: hello thao4 (and hello all :D), you should [id://http:// perlmonks.org/ index.pl?node_id= 479#post|open a question on Seekers of Perl Wisdom], be sure to include whatever you have tried that didn't work, or only does the job partially
[thao4]: yes, I do a little perl since some years but not fréquently, I can correct the triggers in Clearcase of IBM, but write the code : no
[Eily]: meh, such good link ...
[Eily]: here

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2018-02-21 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (276 votes). Check out past polls.

    Notices?