Erlang Programming/Bitsyntax

      Bit Strings

      Erlang lets us use Bit Strings. They have the form

      <<Value:Bitlength>> or <<v1:length1,v2:length2,...>>
      

      The default bit length is 8.

      65> <<1:8>> == <<1>>. 
      true
      

      Bit strings are padded on the left.

      66> <<1:8>> == <<00000001>>. 
      true
      

      Some bit strings have string-like representations.

      38> <<00011111>>.
      <<"g">>
      

      We can select parts of a bit string with pattern matching.

      45> <<H:2,T:6>> = <<"A">>. 
      <<"A">>
      46> H. 
      1
      47> T.
      1 
      86> <<01000001>> == <<"A">>.
      true
      87> <<1:2,1:6>> == <<"A">>.
      true
      88> <<65>> == <<"A">>.
      true
      

      We can match X to a value.

      95> <<1:2,X:6>> = <<"A">>. 
      <<"A">>
      96> X.
      1
      

      But we can not match Y to a bit length.

      97> <<1:2,1:Y>> = <<"A">>.
      ** 1: variable 'Y' is unbound **
      
      Last modified on 4 May 2009, at 04:27