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.
|
---|
In Section
Seekers of Perl Wisdom