albert has asked for the wisdom of the Perl Monks concerning the following question:
I have a series of strings containing 0s and 1s where I am counting the orientation of the 0/1 via bitwise operators. I am doing this as follows;
However, I have large numbers of these strings, and they are much longer than this example code. For storage reasons, they are stored packed as follows:my $string1 = '01010110111000'; my $string2 = '11101101100010'; my ( $c00, $c01, $c10, $c11 ) = ( ( $string1 | $string2 ) =~ tr[0][0], # count 00 ( ~$string1 & $string2 ) =~ tr[\1][\1], # count 01 ( $string1 & ~$string2 ) =~ tr[\1][\1], # count 10 ( $string1 & $string2 ) =~ tr[1][1], # count 11 );
In my application, prior to the counting above, I unpack the strings as follows:my $len = length $unpacked_string; my $packed_string = pack qq{b$len}, $unpacked_string;
I was wondering if there is a way to do the 0/1 counts directly on the packed version, and could skip the need to unpack into strings.my $unpacked_string = unpack qq{b$len}, $packed_string;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Bitwise operators on binary objects
by jwkrahn (Abbot) on Jan 31, 2011 at 10:17 UTC | |
by bart (Canon) on Jan 31, 2011 at 12:11 UTC | |
by albert (Monk) on Jan 31, 2011 at 13:51 UTC | |
Re: Bitwise operators on binary objects
by eyepopslikeamosquito (Archbishop) on Jan 31, 2011 at 09:25 UTC | |
by Anonyrnous Monk (Hermit) on Jan 31, 2011 at 09:42 UTC | |
Re: Bitwise operators on binary objects
by Anonyrnous Monk (Hermit) on Jan 31, 2011 at 21:31 UTC | |
by PeterCordes (Initiate) on Sep 22, 2017 at 09:31 UTC | |
by albert (Monk) on Feb 01, 2011 at 11:35 UTC |
Back to
Seekers of Perl Wisdom