I need to compare two binary numbers and need perl to return the
number of matching bits.
For example:
$aaa = "10111100";
$bbb = "00101100";
In this case, the number of matching bits is 6.
I know I could split the strings and compare the bits one by one.
However, is there any faster functions for this? I need to compare
millions of such strings with 1000 bits for each.
Thanks a lot!!
Johnson Lau
I think what you need is counting how many ones in the xnor calculation of
two binary numbers
I cannot find xnor in Perl, only using xor, then counting how many ones,
that is the number of different bits
As the binary numbers are of 1000 bits, I think you should use Bit::Vector.
here is the code:
use strict;
use warnings;
use Bit::Vector;
use constant NSIZE => 8;
my $aaa = "10111100";
my $bbb = "00101100";
my $a = Bit::Vector->new(NSIZE);
my $b = Bit::Vector->new(NSIZE);
my $c = Bit::Vector->new(NSIZE);
$a->from_Bin($aaa);
$b->from_Bin($bbb);
$c->Xor($a, $b);
my $count = ( $c->to_Bin() =~ tr/1//);
print $count;
__END__
I did not benchmark to test the speed benefits of this method.
~
> I need to compare two binary numbers and need perl to return the
> number of matching bits.
>
> For example:
>
> $aaa = "10111100";
> $bbb = "00101100";
>
> In this case, the number of matching bits is 6.
perl -Mstrict -Mwarnings -le'
my $b1 = "10111100";
my $b2 = "00101100";
$b2 =~ tr/01/10/;
my $n = ($b1 & $b2) =~ tr/0/0/;
print $n;
'
6
To derive the length, a prepared hash (with 256 keys) will be faster.
perl -Mstrict -Mwarnings -le'
my $b1 = oct "0b" . "10111100";
my $b2 = oct "0b" . "00101100";
my $n = sprintf("%08b", $b1 ^ $b2) =~ tr/0/0/;
print $n;
'
To derive the length, a prepared array (with 256 elements) is much
faster.
--
Affijn, Ruud
"Gewoon is een tijger."
use strict;
use warnings;
use Math::GMPz qw(:mpz);
my $str1 = "10111100";
my $str2 = "00101100";
my $wanted = length($str1) - Rmpz_popcount(Math::GMPz->new($str1, 2) ^
Math::GMPz->new($str2, 2));
print $wanted, "\n";
Cheers,
Rob
After reading the others' replies, I believe this xor operation will do what
you want;
#!/usr/bin/perl
use strict;
use warnings;
my $aaa = "10111100";
my $bbb = "00101100";
my $same = ($aaa ^ $bbb) =~ tr/\0//;
print $same;
Chris
Hello,
> I need to compare two binary numbers and need perl to return the
> number of matching bits.
>
> For example:
>
> $aaa = "10111100";
> $bbb = "00101100";
Those aren't binary numbers, they are strings.
perldoc perlnumber
> In this case, the number of matching bits is 6.
>
> I know I could split the strings and compare the bits one by one.
> However, is there any faster functions for this? I need to compare
> millions of such strings with 1000 bits for each.
If you actually had "binary" numbers then you could use unpack( '%32b*',
$var ) to count bits.
perldoc perlpacktut
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
>> I need to compare two binary numbers and need perl to return the
>> number of matching bits. For example:
>> $aaa = "10111100";
>> $bbb = "00101100";
>
> Those aren't binary numbers, they are strings.
I don't mind reading "binary number" as "some base-two representation of
a number".
The word "number" is often used for an encoded stream of digits.