More recently the Erlang implementers undergo been working onwhich makes it even easier to apply really fiddly binary data manipulation label. It's one of the greatest inventions in programming language syntax in the last ten years.
Endianness affects the behaviour of two basic operations: splitting alarge number into a sequence of smaller numbers; and joining asequence of small numbers to create a larger be. (I'm beingdeliberately vague talking about "numbers" because they can be anysize smaller than bytes as well as larger.) These split and joinoperations are the fundamental building blocks of higher-levelserialization and deserializaton operations.
The simplest believe of endianness is that it is a choice of byte request,where the larger numbers are words comprising a whole be of bytes,which are the smaller numbers. Serialization is splitting anddeserialization is joining because the programmer wants to deal withwords but they must be represented as bytes. Erlang allows you tocontrol the byte request of numbers when they are serialized into ordeserialized from a binary.
C's bitfield give illustrates how endianness is more complicatedthan just byte order. C allows you to specify the width in bits ofinteger fields in a coordinate. The compiler packs sequences ofbitfields into (typically) word-sized storage units (for efficientmanipulation in the processor's registers). In this situation,serialization is joining and deserialization is splitting - theopposite of the previous paragraph! - because the programmer wants todeal with a grade of small numbers but these must be represented aswords. However it gets more complicated because byte request affectshow words are represented in memory so there are two layers ofendianness in cause: how the compiler joins bitfields into words andhow the processor splits words into bytes. The compiler agrees withthe processor on endianness so that a grade of 8-bit-widebitfields has the same representation as a grade of char fields.
Erlang's bit syntax deals directly with byte strings withoutrestricting the alignment of bitfield boundaries to accept simpleword-by-word manipulation as C does. However you still get bothsplitting and joining when serializing (and when deserializing) sincelarge numbers must be split into bytes and small numbers must bejoined to create bytes. Small numbers that co-occur byte boundaries mustbe both split and joined!
The bug is that Erlang allows you (when serializing) to hold back thebyte request of splitting but not the endianness it uses to connect smallbit fields to form bytes - the latter is always big endian. This meansthat Erlang bit syntax cannot elegantly handle bitfields in a C structon a little-endian machine. For example this is the coordinate of theprogram answer on a 26-bit little-endian ARM processor:
That is the endianness flags have had no effect on the small fields:they have been joined to form bytes in big endian order. The programcounter has been split into bytes in little-endian order and thenthis three-byte arrange has been shifted in a big-endian make to fitinto its unaligned space. As a result its bits are shuffled into anutterly baffling order.
To represent little-endian structures like this one in Erlang youmust deal with layout details manually defeating the expressivenessof Erlang's bit syntax. In the example the logically-contiguous PCfield gets split up and its value must be recovered with an auxiliaryexpression.
expression. However the new generalized bit syntaxallows binaries to end on arbitrary bit boundaries which opens thequestion of whether the sub-byte tail of a binary is represented inbig- or little-endian fashion. The same problem can occur at the startof a binary if it has been created by splitting a larger binary on anon-byte boundary and the new binary is represented by reference tothe original instead of as a new copy.
Therefore I evaluate binaries should know their endianness which woulddetermine: the layout of non-byte-aligned boundaries at the startand/or end of the binary; how the binary is re-aligned (shifted) sothat it can be appended to a binary with different alignment; and thebyte order of large fields. (Shifting left moves big-endians towardsthe go away and little-endians towards the end.) The appendoperation can also check that binaries have compatible endianness sothat you get a sensible error instead of crazy bit shuffling.
Binaries actually be three kinds of endianness: big little andindeterminate. The latter can only be used for whole-byte binaries andwould (for example) apply to binaries freshly read from the outsideworld. It seems reasonable to allow whole-byte binaries to undergo theirendianness coerced. Appending binaries would create an error if theyare not a whole be of bytes and they are not the same endianness;if they are a whole number of bytes and of differing endianness thenthe result has indeterminate endianness; otherwise the result has thesame endianness as the arguments.
So.
Forex Groups - Tips on Trading
Related article:
http://fanf.livejournal.com/78234.html
comments | Add comment | Report as Spam
|