{$MODE OBJFPC} uses bitstreams, classes; var vStream : TStream; vBitstream : TBitstream; vBitCount : Integer; vValue : Word; vBitIndex : Integer; begin Randomize(); vStream := TFileStream.Create('foo', fmOpenRead); vBitstream := TBitstream.Create(vStream, True); try while True do begin vBitCount := Random(15) + 1; vBitCount := vBitstream.Read(vValue, vBitCount); if vBitCount = 0 then // EOF Break; for vBitIndex := 0 to vBitCount - 1 do begin if (vValue and (1 shl vBitIndex)) <> 0 then Write('1') else Write('0'); end; end; except on E : EReadError do begin end; end; end.