Bit stream input in Factor My attempt at writing code to input values of arbitrary bitlength based on a bytearray in Factor. Any comments on the correctness or style would be appreciated.

USING: kernel locals accessors math sequences math.bitwise ;

TUPLE: bitreader bytearray curpos curbit ;
: &lt;bitreader&gt; ( bytearray -- bitreader ) 0 0 bitreader boa ;

: advance ( reader -- reader ) 0 &gt;&gt;curbit [ 1 + ] change-curpos ;

: check-advance ( reader -- reader ) dup curbit&gt;&gt; 8 = [ advance ] when ;

:: readsinglevalue ( bits reader -- result ) reader curpos&gt;&gt; reader bytearray&gt;&gt; nth 
                                             reader curbit&gt;&gt; dup bits 1 - + swap bit-range
                                             reader [ bits + ] change-curbit check-advance drop ;

: bitsleft ( reader -- result ) curbit&gt;&gt; 8 swap - ;

DEFER: readbits

:: readmultivalue ( bits reader -- result ) reader bitsleft :&gt; bitsextra
                                            bitsextra reader readsinglevalue
                                            bits bitsextra - reader readbits
                                            bitsextra shift + ;

:: readbits ( bits reader -- result ) reader curbit&gt;&gt; bits + 8 &lt; [ bits reader readsinglevalue ] [ bits reader readmultivalue ] if ;
Honestly, the code seems a bit long for what it does. I'm not suggesting anything crazy like code golf, but is there maybe some way to unify (byte_position, bit_position) so a single call advances one bit at a time? I'm looking for a simple invariant that you maintain across calls. The read* functions appear to be on the verbose side.

You have at least four kinds of values: byte_val, bit_val, byte_pos, bit_pos. I encourage clear consistent naming around those. In particular the comments about leaving "result" on the stack are more vague than I'd like to see.