Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Maybe I just don't understand the question, but assuming  unpack '%32b*', ...   is sufficiently efficient, wouldn't a count on the  'b' template specifier serve to count the set bits from the beginning of the string up to but not including a given bit offset?

>perl -wMstrict -le "my $b = qq{\x01\x03\x07\xff\xfe\xfc}; ;; print ' 1111111111222222222233333333334444444444'; print '0123456789' x 5; print unpack 'b*', $b; print '' ;; for my $offset (0, 1, 7 .. 10, 15 .. 20, 24 .. 28, 30 .. 32) { my $c = unpack qq{%32b$offset}, $b; my $vc = vec_sum($b, $offset); die qq{pack sum $c != vec sum $vc} if $c != $vc; printf qq{before bit at offset %2d: %2d bits set \n}, $offset, $c; } ;; sub vec_sum { my ($v, $p) = @_; ;; my $c = 0; vec($v, $_, 1) and ++$c for 0 .. $p - 1; return $c; } " 1111111111222222222233333333334444444444 01234567890123456789012345678901234567890123456789 100000001100000011100000111111110111111100111111 before bit at offset 0: 0 bits set before bit at offset 1: 1 bits set before bit at offset 7: 1 bits set before bit at offset 8: 1 bits set before bit at offset 9: 2 bits set before bit at offset 10: 3 bits set before bit at offset 15: 3 bits set before bit at offset 16: 3 bits set before bit at offset 17: 4 bits set before bit at offset 18: 5 bits set before bit at offset 19: 6 bits set before bit at offset 20: 6 bits set before bit at offset 24: 6 bits set before bit at offset 25: 7 bits set before bit at offset 26: 8 bits set before bit at offset 27: 9 bits set before bit at offset 28: 10 bits set before bit at offset 30: 12 bits set before bit at offset 31: 13 bits set before bit at offset 32: 14 bits set

Update: Example code changed to include check against  vec sum algorithm given in OP; also made things a bit prettier.


In reply to Re: Efficient bit counting with a twist. by AnomalousMonk
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!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (14)
    As of 2015-07-28 18:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (258 votes), past polls