<?xml version="1.0" encoding="windows-1252"?>
<node id="1015595" title="Re: Efficient bit counting with a twist." created="2013-01-27 18:27:23" updated="2013-01-27 18:27:23">
<type id="11">
note</type>
<author id="401112">
johngg</author>
<data>
<field name="doctext">
&lt;p&gt;I'm not sure how efficient [doc://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.&lt;/p&gt;
&lt;code&gt;
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 ) &amp; $mask;

    return $count;
}
&lt;/code&gt;
&lt;p&gt;The output.&lt;/p&gt;
&lt;code&gt;
0         1         2         3         4         5         6         7         8         
012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0100000101000010010000110100010001000101010001100100011101001000010010010100101001001011
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
&lt;/code&gt;
&lt;p&gt;I hope this is useful.&lt;/p&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-401112"&gt;
&lt;p&gt;Cheers,&lt;/p&gt;&lt;p&gt;JohnGG&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1015564</field>
<field name="parent_node">
1015564</field>
</data>
</node>
