note
quester
<p>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.</p>
<code>
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 );
</code>
<br />
<p><i>
(Update: removed a superfluous pair of parens in the last line.)
</i></p>
1015564
1015564