The bis module implements words for reading bits and bytes from a stream of bytes. The module uses a cell wide internal buffer to make it possible to keep reading from succeeding input streams. The maximum number of bits that can be read per call, is limited to the size of a cell minus one byte. So if a cell is 4 bytes wide, this maximum is 24 bit. Keep in mind that the module does not copy the data for the stream, it only stores a reference to the data.
include ffl/bis.fs \ Create an bit input stream variable bis1 in the dictionary bis-create bis1 \ Give the stream some input data (note: the stream does not make a copy of the data) : bis-ab ( -- c-addr u ) s" ab" ; bis-ab bis1 bis-set \ Try reading 10 bits from the stream 10 bis1 bis-need-bits [IF] .( There are 10 bits available in the buffer. ) cr [ELSE] .( There are not enough bits availabe in the buffer, refill.) cr [THEN] \ Read the bits from the stream .( The 10 bits from the stream:) 10 bis1 bis-fetch-bits . cr \ Indicate the the 10 bits are processed 10 bis1 bis-next-bits \ Try reading another 10 bits from the stream 10 bis1 bis-need-bits [IF] .( There are another 10 bits available in the buffer.) cr [ELSE] .( There are not enough bits available in the buffer, refill.) cr [THEN] \ Refill the input stream : bis-cd ( -- c-addr u) s" cd" ; bis-cd bis1 bis-set \ Again try reading another 10 bits from the stream 10 bis1 bis-need-bits [IF] .( There are another 10 bits available in the buffer.) cr [ELSE] .( There are not enough bits available in the buffer, refill.) cr [THEN] \ Read the bits from the stream and indicate that the bits are processed .( The 10 bits from the stream:) 10 bis1 bis-fetch-bits . cr \ Indicate the the 10 bits are processed 10 bis1 bis-next-bits