Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

comparing two binary numbers

15 views
Skip to first unread message

Johnson Lau

unread,
May 11, 2008, 1:09:05 AM5/11/08
to begi...@perl.org
Dear all,

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

Jialin Li

unread,
May 11, 2008, 3:40:09 AM5/11/08
to Johnson Lau, begi...@perl.org
> --
> To unsubscribe, e-mail: beginners-...@perl.org
> For additional commands, e-mail: beginne...@perl.org
> http://learn.perl.org/


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.
~

Dr.Ruud

unread,
May 11, 2008, 6:37:18 AM5/11/08
to begi...@perl.org
"Johnson Lau" schreef:

> 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."

Sisyphus

unread,
May 11, 2008, 6:02:01 AM5/11/08
to begi...@perl.org
On May 11, 3:09 pm, johnson...@cuhk.edu.hk (Johnson Lau) wrote:
> Dear all,
>
> 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.
>

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

Chris Charley

unread,
May 11, 2008, 4:16:38 PM5/11/08
to begi...@perl.org

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


John W. Krahn

unread,
May 12, 2008, 5:02:10 AM5/12/08
to Perl Beginners
Johnson Lau wrote:
> Dear all,

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

Dr.Ruud

unread,
May 12, 2008, 9:40:17 AM5/12/08
to begi...@perl.org
"John W. Krahn" schreef:
> Johnson Lau:

>> 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.

0 new messages