http://www.perlmonks.org?node_id=1015566


in reply to Efficient bit counting with a twist.

Build a bit mask for the byte that contains the position. Then count the bits in the bytes before the position, and do a bitwise and for the byte that contains the position.

my $mask = pack "V", 2 ** ($p % 8) - 1; my $c = unpack( '%32b*', substr( $v, 0, $p / 8 ) ) + unpack( '%32b*', substr( $v, $p / 8, 1 ) & $mask );

(Update: removed a superfluous pair of parens in the last line.)

Replies are listed 'Best First'.
Re^2: Efficient bit counting with a twist.
by BrowserUk (Patriarch) on Jan 27, 2013 at 08:33 UTC

    Why pack 'V'?

    substr( $v, $p / 8, 1 ) is a byte, why mask with 4 bytes?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
    div class=Why
      Oh. That. It's just force of habit, I adapted this bit of code from the way I code bitmasks for IPv4 subnets. It would make more sense to code it as pack 'C',... rather than pack 'V', although it works either way.