[Haskell-cafe] Re: Bit Streams

Adam Langley agl at imperialviolet.org
Wed Jun 18 12:49:26 EDT 2008

On Wed, Jun 18, 2008 at 12:46 AM, Dominic Steinitz
<dominic.steinitz at blueyonder.co.uk> wrote:
> OK, so today I tried to write my first program using the Binary library.
> And I've hit a snag: It appears the library will only handle data that
> is byte-aligned. So if I try to write three Bool values, it uses three
> bytes, not three bits.

There's a BitGet[1] and BitPut in the Hackage version of
binary-strict. However, these versions work on strict ByteStrings.
Putting, for one, is pretty inefficient. In the darcs version[2] of
binary-strict, there's a fully lazy BitPut[3].

There was a request for a lazy BitGet, but I never wrote it. Most of
the time, bit fields are small and you can use the lazy Get from
binary to extract a ByteString and parse that with strict BitGet.
However, the BitPut should be just fine (including doing things like
writing ByteStrings by shifting each byte etc).

If it's missing anything obvious, ping me and I'll see what I can do.


[1] http://darcs.imperialviolet.org/darcsweb.cgi?r=binary-strict;a=headblob;f=/src/Data/Binary/Strict/BitGet.hs
[2] http://darcs.imperialviolet.org/binary-strict/
[3] http://darcs.imperialviolet.org/darcsweb.cgi?r=binary-strict;a=headblob;f=/src/Data/Binary/BitPut.hs

Adam Langley agl at imperialviolet.org http://www.imperialviolet.org

More information about the Haskell-Cafe mailing list