Perl: the Markov chain saw PerlMonks

### Comment on

 Need Help??

I'm not sure how efficient substr is with large strings but it generally seems pretty fast. Using it to count set bits in the whole bytes before your position and then, if necessary, those bits in the partial byte up to but not including it via a mask might be viable.

```use strict;
use warnings;

use 5.014;

say join q{}, map { sprintf q{%-10s}, \$_ } 0 .. 8;
say qq{@{ [ join q{}, 0 .. 9 ] }} x 9;

my \$vec = pack q{C*}, map ord, q{A} .. q{K};
say unpack q{B*}, \$vec;
say qq{Total set bits - @{ [ unpack q{%32b*}, \$vec ] }};

say qq{Set bits to \$_ - @{ [ setBitsB4pos( \ \$vec, \$_ ) ] }}
for 70 .. 87;

sub setBitsB4pos
{
my( \$rsVec, \$pos ) = @_;
my \$wholeBytes = int \$pos / 8;
my \$oddBits    = \$pos % 8;
my \$count      = unpack q{%32b*},
substr \${ \$rsVec }, 0, \$wholeBytes;

return \$count unless \$oddBits;

my \$mask = pack q{C*}, ( 0 ) x \$wholeBytes, do {
my \$acc;
\$acc += 2 ** ( 8 - \$_ ) for 1 .. \$oddBits;
\$acc;
};
\$count += unpack q{%32b*},
substr( \${ \$rsVec }, 0, \$wholeBytes + 1 ) & \$mask;

return \$count;
}

The output.

```0         1         2         3         4         5         6
+7         8
0123456789012345678901234567890123456789012345678901234567890123456789
+01234567890123456789
0100000101000010010000110100010001000101010001100100011101001000010010
+010100101001001011
Total set bits - 31
Set bits to 70 - 23
Set bits to 71 - 23
Set bits to 72 - 24
Set bits to 73 - 24
Set bits to 74 - 25
Set bits to 75 - 25
Set bits to 76 - 25
Set bits to 77 - 26
Set bits to 78 - 26
Set bits to 79 - 27
Set bits to 80 - 27
Set bits to 81 - 27
Set bits to 82 - 28
Set bits to 83 - 28
Set bits to 84 - 28
Set bits to 85 - 29
Set bits to 86 - 29
Set bits to 87 - 30

I hope this is useful.

Cheers,

JohnGG

In reply to Re: Efficient bit counting with a twist. by johngg
in thread Efficient bit counting with a twist. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 [trippledubs]: man I thought Gone With the Wind was about the Civil war

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-02-20 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (267 votes). Check out past polls.

Notices?