8.24  Bit String Comprehensions

8.24 Bit String Comprehensions

Bit string comprehensions are analogous to List Comprehensions. They are used to generate bit strings efficiently and succinctly.

Bit string comprehensions are written with the following syntax:

<< BitStringExpr || Qualifier1,...,QualifierN >>

BitStringExpr is an expression that evalutes to a bit string. If BitStringExpr is a function call, it must be enclosed in parentheses. Each Qualifier is either a generator, a bit string generator or a filter.

  • A generator is written as: Pattern <- ListExpr. ListExpr must be an expression that evaluates to a list of terms.
  • A bit string generator is written as: BitstringPattern <= BitStringExpr. BitStringExpr must be an expression that evaluates to a bitstring.
  • A filter is an expression that evaluates to true or false.The variables in the generator patterns, shadow variables in the function clause, surrounding the bit string comprehensions.

A bit string comprehension returns a bit string, which is created by concatenating the results of evaluating BitString for each combination of bit string generator elements, for which all filters are true.

Example:

1> << << (X*2) >> ||
<<X>> <= << 1,2,3 >> >>.
<<2,4,6>>

More examples are provided in Programming Examples.