[racket] Low level manipulation on numbers

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sat Oct 8 13:57:38 EDT 2011

At Sat, 8 Oct 2011 17:41:35 +0100, "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.
> 
> I can not find an implementation of a non-arithmetic shift for fixnums, and
> a way to specify which size of fixnum to construct.

There aren't different sizes of fixnums. When you want to convert an
exact integer to bytes, then `integer->integer-bytes' lets you specify
the size.

There also isn't a logical shift operator, if I understand what you're
after. If you work with positive integers, then `arithmetic-shift'
(plus `bitwise-and'?) behaves like a logical shift. If you want
logical-shift effects for finite 2's complement representations, then
you probably don't really want to work on fixnum representations, which
are 63 or 31 bits, but instead some power of 2 via bitwise operations
like `bitwise-ior', `bitwise-and', and arithmetic shifts of negative
numbers. Note also that `integer-bytes->integer' lets you say whether
to interpret a set of bytes as unsigned or as 2's complement.

> I can't also find an easy way to get the byte representation of flonums.

`real->floating-point-bytes'



Posted on the users mailing list.