4 msglog missing images
2 msgdatabase Cgi query's
3 msgFile opening problem
3 msg$^I
4 msgCan't run PERL scripts on a shared server?

comparing two binary numbers
\ Johnson Lau (11 May 2008)
. \ Li, Jialin (11 May 2008)
. \ Dr.Ruud (11 May 2008)
. \ sisyphus (11 May 2008)
. \ Chris Charley (11 May 2008)
. \ John W. Krahn (12 May 2008)
. \ Dr.Ruud (12 May 2008)

8 msgPlease help critcize and shorten my sub code
3 msgsplit slice question
4 msgperl-support for vim help needed
4 msgText to hex
1 msgPerl MQSeries API newbie help
3 msgdns error or module error?
3 msgscript for an mbox
4 msgdeleting a user name from a file
1 msgDocumentation problem
5 msgone liner for for and if
3 msgerror in printing unicode
2 msginstall module problem
1 msgCan't get Text::Balanced::extract_quotelike to ...
3 msgRegex help?
Subject:Re: comparing two binary numbers
Group:Perl-beginners
From:Chris Charley
Date:11 May 2008


 

----- Original Message -----
From: ""Johnson Lau"" <johnsonlau>
Newsgroups: perl.beginners
To: <beginners>
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



--
To unsubscribe, e-mail: beginners-unsubscribe
For additional commands, e-mail: beginners-help
http://learn.perl.org/




© 2004-2008 readlist.com