# Erlang Binaries Cheat Sheet

**URL:** <https://erlangforums.com/t/erlang-binaries-cheat-sheet/2327>\
**Category:** Guides/Tuts/Tips/Info\
**Tags:** binaries\
**Created:** [January 31, 2023, 10:42am UTC](https://erlangforums.com/t/erlang-binaries-cheat-sheet/2327 "2023-01-31T10:42:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fylke](https://erlangforums.com/user_avatar/erlangforums.com/fylke/32/1729_2.png) [@Fylke](https://erlangforums.com/u/Fylke)\
**Post date:** [January 31, 2023, 10:42am UTC](https://erlangforums.com/t/erlang-binaries-cheat-sheet/2327/1 "2023-01-31T10:42:14Z")

</div>

I updated my cheat sheet for binaries: [https://cheatography.com/fylke/cheat-sheets/erlang-binaries/](https://cheatography.com/fylke/cheat-sheets/erlang-binaries/)

Every time I’m working with binaries, it feels like I’ve forgotten all the related syntax. So I made a cheat sheet. Hopefully you will find it helpful too!

I’m struggling a bit with additional examples of just normal use - any suggestions are welcome!

---

<div class="post-metadata">

**Author:** ![phild](https://erlangforums.com/user_avatar/erlangforums.com/phild/32/742_2.png) [@phild](https://erlangforums.com/u/phild)\
**Post date:** [January 31, 2023, 1:56pm UTC](https://erlangforums.com/t/erlang-binaries-cheat-sheet/2327/2 "2023-01-31T13:56:04Z")

</div>

A trivial example I used in Advent of Code:

```erlang
parse(<<"noop">>) -> noop;
parse(<<"addx ", V/binary>>) -> {addx, binary_to_integer(V)}.

```

---

<div class="post-metadata">

**Author:** ![sverker](https://erlangforums.com/user_avatar/erlangforums.com/sverker/32/841_2.png) [@sverker](https://erlangforums.com/u/sverker)\
**Post date:** [February 10, 2023, 9:18am UTC](https://erlangforums.com/t/erlang-binaries-cheat-sheet/2327/3 "2023-02-10T09:18:40Z")

</div>

These two examples do not work:

| `<<A­/bi­g>> = <<255, 0>>` | `255` |
| --- | --- |
| `<<A­/li­ttl­e>> = <<255, 0>>` | `65280` |

You must specify bit count, and the big/little endians are reversed. This works:

| `<<A­:16/big>> = <<255, 0>>` | `65280` |
| --- | --- |
| `<<A­:16/li­ttl­e>> = <<255, 0>>` | `255` |

---

<div class="post-metadata">

**Author:** ![Fylke](https://erlangforums.com/user_avatar/erlangforums.com/fylke/32/1729_2.png) [@Fylke](https://erlangforums.com/u/Fylke)\
**Post date:** [February 10, 2023, 2:43pm UTC](https://erlangforums.com/t/erlang-binaries-cheat-sheet/2327/4 "2023-02-10T14:43:17Z")

</div>

Thank you, fixed!

---

<div class="post-metadata">

**Author:** ![RMorisset](https://erlangforums.com/user_avatar/erlangforums.com/rmorisset/32/1601_2.png) [@RMorisset](https://erlangforums.com/u/RMorisset)\
**Post date:** [February 15, 2023, 11:43am UTC](https://erlangforums.com/t/erlang-binaries-cheat-sheet/2327/5 "2023-02-15T11:43:17Z")

</div>

I have a couple of nitpicks about the Segments section:

- " `Value` is either a literal or a variable"
- “`Size` […] can be any expression that evaluates to an `integer`”

The rules are a bit more subtle (and given in [Erlang -- Expressions](https://www.erlang.org/doc/reference_manual/expressions.html#bit-syntax-expressions)):

- If the binary is a pattern used for matching
  - `Value` must indeed be a literal or a variable
  - But `Size` must be a guard expression

- If the binary is an expression
  - `Size` can indeed be any expression that evaluates to an integer
  - But `Value` can be any expression, as long as it is wrapped in parenthesis.

So for example the following is fine:

```erlang
f(X) ->
  <<(case X of [] -> 42; _ -> 13 end):16/big>>.

```

But the following is not:

```erlang
f(<<X:(foobar())/big>>) ->
  X.

```

---

<div class="post-metadata">

**Author:** ![Fylke](https://erlangforums.com/user_avatar/erlangforums.com/fylke/32/1729_2.png) [@Fylke](https://erlangforums.com/u/Fylke)\
**Post date:** [February 15, 2023, 3:04pm UTC](https://erlangforums.com/t/erlang-binaries-cheat-sheet/2327/6 "2023-02-15T15:04:25Z")

</div>

Oooh, good one, thanks! I’ll fix that

---

<div class="post-metadata">

**Author:** ![Fylke](https://erlangforums.com/user_avatar/erlangforums.com/fylke/32/1729_2.png) [@Fylke](https://erlangforums.com/u/Fylke)\
**Post date:** [July 31, 2025, 3:17pm UTC](https://erlangforums.com/t/erlang-binaries-cheat-sheet/2327/7 "2025-07-31T15:17:51Z")

</div>

With the help of @chiroptical, the cheat sheet now has a new and improved home (and look) over at github: [GitHub - chiroptical/erlang-binary-cheatsheet: Converting https://cheatography.com/fylke/cheat-sheets/erlang-binaries to typst with PDF release](https://github.com/chiroptical/erlang-binary-cheatsheet)

Let me know if you find any mistakes.
