[racket] Low level manipulation on numbers

From: Tony Garnock-Jones (tonyg at ccs.neu.edu)
Date: Tue Oct 11 10:46:42 EDT 2011

Hi Nicolas,

On 2011-10-08 12:41 PM, nicolas.oury at gmail.com wrote:
> I am trying to write a small implementation of Protocol Buffers
> (   http://code.google.com/p/protobuf/  ) and I have difficulties
> with bit manipulations, especially to transform a negative integer into
> a series of bytes and back.

You might also be interested to try out (require (planet
tonyg/bitsyntax)). The documentation is at
http://planet.racket-lang.org/package-source/tonyg/bitsyntax.plt/3/2/planet-docs/manual/index.html

If, for example, you wanted to parse a little-endian 32-bit length
prefixed blob from a packet, you might write

(bit-string-case packet
  ([ (len :: little-endian integer bits 32)
     (body :: binary bytes len) ]
   body))

The library supports arbitrary-width integers, and 32- or 64-bit floats.
It uses the primitives that Matthew mentioned internally.

Regards,
  Tony


Posted on the users mailing list.