They're real nice and deserve more play.
They also have some utility outside of just putting together and taking apart bit streams. What if each struct had a bitpattern that represented it, and you could cast between structs that had matching bitpatterns?
Ok, so the way Erlang does it is this: We have a bitfield of
<< E1, E2, E3, ... >>
It can be used for constructing values or matching patterns. All elements must be bound. Each elementE
can be followed by a a colon and a size, then a slash and a property list. SoE:16
says thatE
is 16 bits.E:16/integer
is a 16 bit integer,E:16/integer-little
is a 16 bit little-endian integer, etc.Pretty straightforward. A lot of the fanciness comes from the pattern matching you can do atop it.
The properties you have are:
- Type. Integer, float, binary (aka array of bytes).
- Signedness. Only matters for integers, can be signed or unsigned.
- Endianness. Can be big, little, or native.
- Unit. A little wonky, basically a multiplier for the size and does something with alignment as well.
Oh, superceded by #63