From f98300fbe9bb29eb3eb2182b12094974a6dc195b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Thu, 12 Mar 2015 15:35:13 +0100 Subject: Update Programming Examples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Language cleaned up by the technical writers xsipewe and tmanevik from Combitech. Proofreading and corrections by Björn Gustavsson. --- system/doc/programming_examples/bit_syntax.xml | 210 +++++++++++++------------ 1 file changed, 110 insertions(+), 100 deletions(-) (limited to 'system/doc/programming_examples/bit_syntax.xml') diff --git a/system/doc/programming_examples/bit_syntax.xml b/system/doc/programming_examples/bit_syntax.xml index fb321c1ba9..7ede5b71f9 100644 --- a/system/doc/programming_examples/bit_syntax.xml +++ b/system/doc/programming_examples/bit_syntax.xml @@ -31,62 +31,64 @@
Introduction -

In Erlang a Bin is used for constructing binaries and matching +

In Erlang, a Bin is used for constructing binaries and matching binary patterns. A Bin is written with the following syntax:

>]]> -

A Bin is a low-level sequence of bits or bytes. The purpose of a Bin is - to be able to, from a high level, construct a binary,

+

A Bin is a low-level sequence of bits or bytes. + The purpose of a Bin is to enable construction of binaries:

>]]> -

in which case all elements must be bound, or to match a binary,

+

All elements must be bound. Or match a binary:

> = Bin ]]> -

where Bin is bound, and where the elements are bound or +

Here, Bin is bound and the elements are bound or unbound, as in any match.

-

In R12B, a Bin need not consist of a whole number of bytes.

+

Since Erlang R12B, a Bin does not need to consist of a whole number of bytes.

A bitstring is a sequence of zero or more bits, where - the number of bits doesn't need to be divisible by 8. If the number + the number of bits does not need to be divisible by 8. If the number of bits is divisible by 8, the bitstring is also a binary.

Each element specifies a certain segment of the bitstring. A segment is a set of contiguous bits of the binary (not necessarily on a byte boundary). The first element specifies the initial segment, the second element specifies the following - segment etc.

-

The following examples illustrate how binaries are constructed + segment, and so on.

+

The following examples illustrate how binaries are constructed, or matched, and how elements and tails are specified.

Examples -

Example 1: A binary can be constructed from a set of +

Example 1: A binary can be constructed from a set of constants or a string literal:

>, Bin12 = <<"abc">>]]> -

yields binaries of size 3; binary_to_list(Bin11) - evaluates to [1, 17, 42], and - binary_to_list(Bin12) evaluates to [97, 98, 99].

-

Example 2: Similarly, a binary can be constructed +

This gives two binaries of size 3, with the following evaluations:

+ + binary_to_list(Bin11) evaluates to [1, 17, 42]. + binary_to_list(Bin12) evaluates to [97, 98, 99]. + +

Example 2:Similarly, a binary can be constructed from a set of bound variables:

>]]> -

yields a binary of size 4, and binary_to_list(Bin2) - evaluates to [1, 17, 00, 42] too. Here we used a - size expression for the variable C in order to +

This gives a binary of size 4. + Here, a size expression is used for the variable C to specify a 16-bits segment of Bin2.

-

Example 3: A Bin can also be used for matching: if +

binary_to_list(Bin2) evaluates to [1, 17, 00, 42].

+

Example 3: A Bin can also be used for matching. D, E, and F are unbound variables, and - Bin2 is bound as in the former example,

+ Bin2 is bound, as in Example 2:

> = Bin2]]> -

yields D = 273, E = 00, and F binds to a binary +

This gives D = 273, E = 00, and F binds to a binary of size 1: binary_to_list(F) = [42].

Example 4: The following is a more elaborate example - of matching, where Dgram is bound to the consecutive - bytes of an IP datagram of IP protocol version 4, and where we - want to extract the header and the data of the datagram:

+ of matching. Here, Dgram is bound to the consecutive + bytes of an IP datagram of IP protocol version 4. The ambition is + to extract the header and the data of the datagram:

> = RestDgram, ... end.]]> -

Here the segment corresponding to the Opts variable - has a type modifier specifying that Opts should +

Here, the segment corresponding to the Opts variable + has a type modifier, specifying that Opts is to bind to a binary. All other variables have the default type equal to unsigned integer.

-

An IP datagram header is of variable length, and its length - - measured in the number of 32-bit words - is given in - the segment corresponding to HLen, the minimum value of - which is 5. It is the segment corresponding to Opts - that is variable: if HLen is equal to 5, Opts - will be an empty binary.

+

An IP datagram header is of variable length. This length is + measured in the number of 32-bit words and is given in + the segment corresponding to HLen. The minimum value of + HLen is 5. It is the segment corresponding to Opts + that is variable, so if HLen is equal to 5, Opts + becomes an empty binary.

The tail variables RestDgram and Data bind to - binaries, as all tail variables do. Both may bind to empty + binaries, as all tail variables do. Both can bind to empty binaries.

-

If the first 4-bits segment of Dgram is not equal to - 4, or if HLen is less than 5, or if the size of - Dgram is less than 4*HLen, the match of - Dgram fails.

+

The match of Dgram fails if one of the following occurs:

+ + The first 4-bits segment of Dgram is not equal to 4. + HLen is less than 5. + The size of Dgram is less than 4*HLen. +
- A Lexical Note -

Note that ">]]>" will be interpreted as + Lexical Note +

Notice that ">]]>" will be interpreted as ">]]>", which is a syntax error. - The correct way to write the expression is - ">]]>".

+ The correct way to write the expression is: + >]]>.

Segments

Each segment has the following general syntax:

Value:Size/TypeSpecifierList

-

Both the Size and the TypeSpecifier or both may be - omitted; thus the following variations are allowed:

-

Value

-

Value:Size

-

Value/TypeSpecifierList

-

Default values will be used for missing specifications. - The default values are described in the section +

The Size or the TypeSpecifier, or both, can be + omitted. Thus, the following variants are allowed:

+ + Value + Value:Size + Value/TypeSpecifierList + +

Default values are used when specifications are missing. + The default values are described in Defaults.

-

Used in binary construction, the Value part is any - expression. Used in binary matching, the Value part must - be a literal or variable. You can read more about - the Value part in the section about constructing - binaries and matching binaries.

+

The Value part is any expression, when used in binary construction. + Used in binary matching, the Value part must + be a literal or a variable. For more information about + the Value part, see + Constructing Binaries and Bitstrings + and + Matching Binaries.

The Size part of the segment multiplied by the unit in - the TypeSpecifierList (described below) gives the number + TypeSpecifierList (described later) gives the number of bits for the segment. In construction, Size is any expression that evaluates to an integer. In matching, Size must be a constant expression or a variable.

@@ -160,22 +168,22 @@ end.]]> binary. Signedness The signedness specification can be either signed - or unsigned. Note that signedness only matters for + or unsigned. Notice that signedness only matters for matching. Endianness The endianness specification can be either big, little, or native. Native-endian means that - the endian will be resolved at load time to be either + the endian is resolved at load time, to be either big-endian or little-endian, depending on what is "native" for the CPU that the Erlang machine is run on. Unit The unit size is given as unit:IntegerLiteral. - The allowed range is 1-256. It will be multiplied by + The allowed range is 1-256. It is multiplied by the Size specifier to give the effective size of - the segment. In R12B, the unit size specifies the alignment - for binary segments without size (examples will follow). + the segment. Since Erlang R12B, the unit size specifies the alignment + for binary segments without size. -

Example:

+

Example:

X:4/little-signed-integer-unit:8

This element has a total size of 4*8 = 32 bits, and it contains @@ -184,13 +192,14 @@ X:4/little-signed-integer-unit:8

Defaults -

The default type for a segment is integer. The default +

The default type for + a segment is integer. The default type does not depend on the value, even if the value is a - literal. For instance, the default type in '>]]>' is + literal. For example, the default type in >]]> is integer, not float.

The default Size depends on the type. For integer it is 8. For float it is 64. For binary it is all of the binary. In - matching, this default value is only valid for the very last + matching, this default value is only valid for the last element. All other binary elements in matching must have a size specification.

The default unit depends on the the type. For integer, @@ -201,61 +210,60 @@ X:4/little-signed-integer-unit:8

Constructing Binaries and Bitstrings +

This section describes the rules for constructing binaries using the bit syntax. Unlike when constructing lists or tuples, the construction of a binary can fail with a badarg exception.

There can be zero or more segments in a binary to be - constructed. The expression '>]]>' constructs a zero + constructed. The expression >]]> constructs a zero length binary.

Each segment in a binary can consist of zero or more bits. There are no alignment rules for individual segments of type integer and float. For binaries and bitstrings without size, the unit specifies the alignment. Since the default alignment for the binary type is 8, the size of a binary - segment must be a multiple of 8 bits (i.e. only whole bytes). - Example:

+ segment must be a multiple of 8 bits, that is, only whole bytes.

+

Example:

>]]>

The variable Bin must contain a whole number of bytes, because the binary type defaults to unit:8. - A badarg exception will be generated if Bin would - consist of (for instance) 17 bits.

+ A badarg exception is generated if Bin + consist of, for example, 17 bits.

-

On the other hand, the variable Bitstring may consist of - any number of bits, for instance 0, 1, 8, 11, 17, 42, and so on, - because the default unit for bitstrings is 1.

+

The Bitstring variable can consist of + any number of bits, for example, 0, 1, 8, 11, 17, 42, and so on. + This is because the default unit for bitstrings is 1.

-

For clarity, it is recommended not to change the unit - size for binaries, but to use binary when you need byte - alignment, and bitstring when you need bit alignment.

+

For clarity, it is recommended not to change the unit + size for binaries. Instead, use binary when you need byte alignment + and bitstring when you need bit alignment.

-

The following example

+

The following example successfully constructs a bitstring of 7 bits, + provided that all of X and Y are integers:

>]]> -

will successfully construct a bitstring of 7 bits. - (Provided that all of X and Y are integers.)

-

As noted earlier, segments have the following general syntax:

+

As mentioned earlier, segments have the following general syntax:

Value:Size/TypeSpecifierList

When constructing binaries, Value and Size can be any Erlang expression. However, for syntactical reasons, both Value and Size must be enclosed in parenthesis if the expression consists of anything more than a single literal - or variable. The following gives a compiler syntax error:

+ or a variable. The following gives a compiler syntax error:

>]]> -

This expression must be rewritten to

+

This expression must be rewritten into the following, + to be accepted by the compiler:

>]]> -

in order to be accepted by the compiler.

Including Literal Strings -

As syntactic sugar, an literal string may be written instead - of a element.

+

A literal string can be written instead of an element:

>]]> -

which is syntactic sugar for

+

This is syntactic sugar for the following:

>]]>
@@ -263,29 +271,30 @@ X:4/little-signed-integer-unit:8
Matching Binaries -

This section describes the rules for matching binaries using + +

This section describes the rules for matching binaries, using the bit syntax.

There can be zero or more segments in a binary pattern. - A binary pattern can occur in every place patterns are allowed, - also inside other patterns. Binary patterns cannot be nested.

-

The pattern '>]]>' matches a zero length binary.

-

Each segment in a binary can consist of zero or more bits.

-

A segment of type binary must have a size evenly - divisible by 8 (or divisible by the unit size, if the unit size has been changed).

-

A segment of type bitstring has no restrictions on the size.

-

As noted earlier, segments have the following general syntax:

+ A binary pattern can occur wherever patterns are allowed, + including inside other patterns. Binary patterns cannot be nested. + The pattern >]]> matches a zero length binary.

+

Each segment in a binary can consist of zero or more bits. + A segment of type binary must have a size evenly divisible by 8 + (or divisible by the unit size, if the unit size has been changed). + A segment of type bitstring has no restrictions on the size.

+

As mentioned earlier, segments have the following general syntax:

Value:Size/TypeSpecifierList

-

When matching Value value must be either a variable or - an integer or floating point literal. Expressions are not +

When matching Value, value must be either a variable or + an integer, or a floating point literal. Expressions are not allowed.

Size must be an integer literal, or a previously bound - variable. Note that the following is not allowed:

+ variable. The following is not allowed:

>) -> {X,T}.]]>

The two occurrences of N are not related. The compiler will complain that the N in the size field is unbound.

-

The correct way to write this example is like this:

+

The correct way to write this example is as follows:

<> = Bin, @@ -303,14 +312,14 @@ foo(<>) ->]]> without size:

>) ->]]> -

There is no restriction on the number of bits in the tail.

+

There are no restrictions on the number of bits in the tail.

Appending to a Binary -

In R12B, the following function for creating a binary out of - a list of triples of integers is now efficient:

+

Since Erlang R12B, the following function for creating a binary out of + a list of triples of integers is efficient:

triples_to_bin(T, <<>>). @@ -321,7 +330,8 @@ triples_to_bin([], Acc) -> Acc.]]>

In previous releases, this function was highly inefficient, because the binary constructed so far (Acc) was copied in each recursion step. - That is no longer the case. See the Efficiency Guide for more information.

+ That is no longer the case. For more information, see + Efficiency Guide.

-- cgit v1.2.3