use strict;


sub write_bits( $$$$ )
{
    my ($out, $l, $num, $q) = @_;
    $l .= substr(unpack("B*", pack("N", $num)),
         -$q);
    if ( (length($l) > 8) ) {
    my $left = substr($l, 8);
    print $out pack("B8", $l);
    $l = $left;
    }
    return $l;
}


sub flush_bits( $$ )
{
    my ($out, $b) = @_;
    print $out pack("B*", $b);
}


sub read_bits( $$$ )
{
    my ( $in, $b, $n ) = @_;
    
    
    
    if ( $n > 32 ) { return 0; }
    while ( length($b) < $n ) {
    my $v;
    my $red = read($in, $v, 1);
    if ( $red < 1 ) { return ( 0, -1 ); }
    $b .= substr(unpack("B*", $v), -8);
    }
    my $bits = "0" x ( 32-$n ) . substr($b, 0, $n);
    my $val = unpack("N", pack("B32", $bits));
    $b = substr($b, $n);
    return ($val, $b);
}