Bit syntax frustrations, again

James Hague jamesh@REDACTED
Wed Sep 25 19:31:02 CEST 2002


I've been writing an Erlang module to decode swf (Flash) files.  The swf
format is documented at http://www.openswf.org.  The problem I've been
having is that Erlang starts decoding bits with the most significant bit,
whereas the swf format--which is very bit-oriented--starts with the least
significant bit.  As best I can tell, the "little" qualifier in Erlang's bit
syntax refers to bytes in multi-byte values, not bits.

To give an example, suppose the next byte in the input stream contains
16#fe, and I need to grab a 3-bit field followed by a 5-bit field.  The
obvious match of:

	<<Field1:3, Field2:5, _/binary>> = Binary

results in:

	Field1 = 2#111
	Field2 = 2#11110

In the swf format, though, the correct answer is:

	Field1 = 2#110
	Field2 = 2#11111

In this simple case there's an easy enough fix, but not for the general case
when bit fields cross byte boundaries.  I don't see a nice fix in that case.
I'm guessing I'll have to put aside the bit syntax for most of it and do bit
manipulations directly.  Bleah.  Any thoughts we could appreciated.



More information about the erlang-questions mailing list