Bit syntax endianness confusion

Tony Rogvall <>
Mon Dec 4 22:43:00 CET 2000


James Hague wrote:

> 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

This "problem" was addressed by us (klacke and me) in the original proposal of
the Bit syntax.
The source of such bit combinations mostly arise from C language bit fields.

Our solution was to have sub group bit syntax expression. Then you could solve
your problem by
writing something like:

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

Expression like yours above does not make much sense (Field1:4/little) since
only byte sequences are
affected by the endian order.

I am sure the OTP team will extend the bit syntax with a lot of nice features
in the future ;-)

/Tony

-------------- next part --------------
A non-text attachment was scrubbed...
Name: tony.vcf
Type: text/x-vcard
Size: 333 bytes
Desc: Card for Tony Rogvall
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20001204/c649c867/attachment.vcf>


More information about the erlang-questions mailing list