[Haskell-cafe] Re: ANN: A triple of new packages for talking tothe outside world

Adam Langley agl at imperialviolet.org
Wed Jan 16 20:33:41 EST 2008

On Jan 16, 2008 2:41 PM, Dominic Steinitz
<dominic.steinitz at blueyonder.co.uk> wrote:
> tlv_ bin =
>    do tagValueVal <- getBits bin 5
>       tagConstructionVal <- getBits bin 1
>       tagTypeVal <- getBits bin 2
> I'm sure I'm wrong but putting bits into [Bool] doesn't look very efficient. Of
> course, NewBinary didn't address what happened for n >= 8. Some possibilities
> are a) not allowing more than 8 b) returning [Word8] or c) (which I thought was
> where we'd go) a ByteString with some sort of padding.

BitGet is just an API RFC at the moment, so I'm just describing it
here - not trying to justify it.

In BitGet there's getAsWord[8|16|32|64] which take a number of bits ($n$) and
returns the next $n$ bits in the bottom of a Word$x$. Thus, getAsWord8 is what
you call getBits and, if you had a 48 bit number, you could use getAsWord64 and
the bottom 48-bits of the resulting Word64 would be what you want.

Equally, asking for more than $x$ bits when calling getAsWord$x$ is a mistake,
however I don't check for it in the interest of speed.

There are also get[Left|Right]ByteString which return the next $n$ bits in a
ByteString of Word8's. The padding is either at the end of the last byte (left
aligned) or at the beginning of the first byte (right aligned).

If you did want a [Bool], you could use:
  bits <- sequence $ take n $ repeat getBit


Adam Langley                                      agl at imperialviolet.org
http://www.imperialviolet.org                       650-283-9641

More information about the Haskell-Cafe mailing list