Re: comparing two binary numbers

[email protected] ("John W. Krahn")
Newsgroups perl.beginners
Message-ID <[email protected]>
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
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.