Given a bit vector $v, this: my $c = unpack '%32b*', $v; will count the set bits very efficiently.
However, I need to count the set bits preceding (not including) a given bit position within $v.
I can do that using:
## $p = position my $c = 0; vec( $v, $_, 1 ) and ++$c for 0 .. $p - 1;
But that is grossly inefficient for large vectors.
Can you do it more efficiently?
Note:The rest of the code is pure perl, so I'd rather avoid going to XS to do this if possible.