~icefox/garnet#34: 
Steal Erlang's bit patterns

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?

Status
RESOLVED CLOSED
Submitter
~icefox
Assigned to
No-one
Submitted
3 years ago
Updated
1 year, 4 months ago
Labels
T-DESIGN

~icefox referenced this from #56 1 year, 10 months ago

~icefox 1 year, 10 months ago

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 element E can be followed by a a colon and a size, then a slash and a property list. So E:16 says that E 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.

~icefox REPORTED CLOSED 1 year, 4 months ago

Oh, superceded by #63

Register here or Log in to comment, or comment via email.