Re: comparing two binary numbers

[email protected] ("Chris Charley")
Newsgroups perl.beginners
Message-ID <002d01c8b3a3$ec384150$3b104845@S0029864230>
----- Original Message ----- 
From: ""Johnson Lau"" <[email protected]>
Newsgroups: perl.beginners
To: <[email protected]>
Sent: Sunday, May 11, 2008 1:09 AM
Subject: comparing two binary numbers


> 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

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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.