     bignum bs bit-pos>> neg shift n bits ;
 
 :: subseq>bits-be ( bignum n bs -- bits )
-    bignum 
+    bignum
     8 bs bit-pos>> - n - 8 mod dup 0 < [ 8 + ] when
     neg shift n bits ;
 


: <lsb0-bit-reader> ( bytes -- bs )
    lsb0-bit-reader new swap >>bytes ; inline

TUPLE: msb0-bit-writer < bit-writer ;
TUPLE: lsb0-bit-writer < bit-writer ;

! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors byte-arrays byte-vectors
combinators.short-circuit fry io.binary kernel locals math
math.bitwise sequences sequences.private ;
IN: bitstreams

TUPLE: widthed
{ bits integer read-only }
{ #bits integer read-only } ;

ERROR: invalid-widthed bits #bits ;

: check-widthed ( bits #bits -- bits #bits )
    2dup {
        [ nip 0 < ]
        [ { [ nip 0 = ] [ drop 0 = not ] } 2&& ]
        [
            swap [ drop f ] [
                dup 0 < [ neg ] when log2 <=
            ] if-zero
        ]
    } 2|| [ invalid-widthed ] when ;

: <widthed> ( bits #bits -- widthed )
    check-widthed
    widthed boa ;

: zero-widthed ( -- widthed ) 0 0 <widthed> ;

: zero-widthed? ( widthed -- ? ) zero-widthed = ;

TUPLE: bit-reader
    { bytes byte-array }
    { byte-pos array-capacity initial: 0 }
    { bit-pos array-capacity initial: 0 } ;

TUPLE: msb0-bit-reader < bit-reader ;
TUPLE: lsb0-bit-reader < bit-reader ;
