Erlang Bit Syntax

Erlang has a special syntax for (de)constructing structured binary data. This is called "the bit syntax".

Here is a code fragment for efficiently parsing the headers out of a IP packet in Erlang:

 <<?IP_VERSION:4, HLen:4, SrvcType:8, TotLen:16, 
  ID:16, Flgs:3, FragOff:13, TTL:8, Proto:8, HdrChkSum:16,
  SrcIP:32, DestIP:32, RestDgram/binary>> = Packet,

(That link is broken; here's one that works:


Question: Does the erlang bit syntax give any assistance with handling structures that contain pointers? That is, is it possible to take some bits, treat them as an address, and access memory at that address?

Answer: Not without writing an extension to allow you to do that. Accessing an arbitrary memory location is an unsafe process.


Question: Does Erlang assume NFS network order?

Answer: Network order is the default.

Question:Is there a way to override any such default byte ordering in packets?

Answer: Yes, you can specify the byte ordering.

CeeLanguage (and CeePlusPlus, ObjectiveCee) has a rather similar feature (the BitField), which is less widely used then one might expect, for two reasons:

The second issue probably doesn't apply to Erlang, but the first clearly does. How are the bits packed in?


See related paper http://www.erlang.se/workshop/2004/esmb.pdf



EditText of this page (last edited July 27, 2014) or FindPage with title or text search