Jian Kang wrote:
>
> I got a problem of finding the difference of two
> bit(binary) strings,
>
> eg:
> $a = "01";
> $b = "11";
>
> differs by: 1
>
> Is there a good way to solve this when $a and $b are
> very long, like:
> $a = "000000111010011000011110000110010"
> $b = "111011010101111100001110010010010"
>
> I used 'vec' function to extract each bit and compare,
> the code is really ugly, please, if you have some
> better idea. Thanks.

Type one hundred times "I must never use $a and $b as
variable names" :)

If you use Jeff's suggestion of bitwise-oring the whole
string you will get a string of bytes with values zero
and one, rather then the characters '0' and '1'. To make
this printable just 'or' in a string of zeroes of the
right length. See below.

Beware that you must make sure that both string are the
same length before you do this, otherwise it won't work.
(Add leading zeroes to the shorter string to correct it.)

HTH,

Rob


use strict;
use warnings;

my $aa = "000000111010011000011110000110010";
my $bb = "111011010101111100001110010010010";
my $dd = $aa ^ $bb;
$dd |= '0' x length $dd;

print map "$_\n", ($aa, $bb, $dd);

**OUTPUT**

000000111010011000011110000110010
111011010101111100001110010010010
111011101111100100010000010100000




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to