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 );