Skip to content

Instantly share code, notes, and snippets.

@svevang
Last active February 5, 2018 18:00
Show Gist options
  • Save svevang/c2bf0b7a7d8e58bc346b5cea311049d8 to your computer and use it in GitHub Desktop.
Save svevang/c2bf0b7a7d8e58bc346b5cea311049d8 to your computer and use it in GitHub Desktop.
Notes on binary pattern matching in Elixir

Elixir

Binary parsing in elixir is based on pattern matching.

<< "this is a binary literal in elixir" >>

So parsing binaries looks like:

iex(4)> << sign::size(1), exp::size(11), mantissa::size(52) >> = << 3.14159::float >>
<<64, 9, 33, 249, 240, 27, 134, 110>>

iex(5)> (1 + mantissa / :math.pow(2, 52)) * :math.pow(2, exp-1023)                   
3.14159
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment