Bit syntax endianness confusion

James Hague jamesh@REDACTED
Thu Nov 30 16:31:13 CET 2000


Suppose I have a binary that contains a 32-bit word stored in big endian
(network) order.  This word contains three fields in this order:  10 bits,
4 bits, and 18 bits.  Matching this is easy:

<<Field0:10,Field1:4,Field2:18>> = Binary.

Now, suppose this word is stored in little endian order.  How would I match
it?  This isn't right:

<<Field0:10/little,Field1:4/little,Field2:18/little>> = Binary.

Or is it?  Actually, how would the following be interpeted:

<<Field0:10/little,Field1:4,Field2:18/little>> = Binary.

where the first and last fields are little endian but the middle one isn't?
 I get the impression that the "little" qualifier reverses bitfields, but I
need something higher level than that.  I want to be able to say that the
entire word is stored in little endian order.

Do I need to do some swizzling first, by interpreting the binary as four
bytes, creating a new binary with the bytes reversed, and then matching
against the original pattern?

James




More information about the erlang-questions mailing list