in reply to Reading individual bits


Here are two variations. The first uses sprintf to do the zero padding. The second uses oct to convert the binary string to decimal (for perl >= 5.6).

For clarity I've omitted the $$OffsetRef part. Using proper bitmasks and the bitwise operators would probably be faster, see perlop.

sub ReadBits2 { my $bitstring = @_[0]; my $offset = @_[1]; my $length = @_[2]; return unpack "N", pack "B*", sprintf "%032s", substr $bitstring, $offset, $length; } sub ReadBits3 { return oct b . substr @_[0], @_[1], @_[2]; }

--
John.