Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: comparing t...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 7 Topic 11053 of 11532
Post > Topic >>

Re: comparing two binary numbers

by krahnj@[EMAIL PROTECTED] (John W. Krahn) May 12, 2008 at 02:02 AM

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
 




 7 Posts in Topic:
comparing two binary numbers
johnsonlau@[EMAIL PROTECT  2008-05-11 13:09:05 
Re: comparing two binary numbers
jialinli1981@[EMAIL PROTE  2008-05-11 02:40:09 
Re: comparing two binary numbers
rvtol+news@[EMAIL PROTECT  2008-05-11 12:37:18 
Re: comparing two binary numbers
sisyphus359@[EMAIL PROTEC  2008-05-11 03:02:01 
Re: comparing two binary numbers
charley@[EMAIL PROTECTED]  2008-05-11 16:16:38 
Re: comparing two binary numbers
krahnj@[EMAIL PROTECTED]   2008-05-12 02:02:10 
Re: comparing two binary numbers
rvtol+news@[EMAIL PROTECT  2008-05-12 15:40:17 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 6:18:56 CDT 2008.