\documentclass[10pt,a4paper]{article}

% Packages
\usepackage{fancyhdr}           % For header and footer
\usepackage{multicol}           % Allows multicols in tables
\usepackage{tabularx}           % Intelligent column widths
\usepackage{tabulary}           % Used in header and footer
\usepackage{hhline}             % Border under tables
\usepackage{graphicx}           % For images
\usepackage{xcolor}             % For hex colours
%\usepackage[utf8x]{inputenc}    % For unicode character support
\usepackage[T1]{fontenc}        % Without this we get weird character replacements
\usepackage{colortbl}           % For coloured tables
\usepackage{setspace}           % For line height
\usepackage{lastpage}           % Needed for total page number
\usepackage{seqsplit}           % Splits long words.
%\usepackage{opensans}          % Can't make this work so far. Shame. Would be lovely.
\usepackage[normalem]{ulem}     % For underlining links
% Most of the following are not required for the majority
% of cheat sheets but are needed for some symbol support.
\usepackage{amsmath}            % Symbols
\usepackage{MnSymbol}           % Symbols
\usepackage{wasysym}            % Symbols
%\usepackage[english,german,french,spanish,italian]{babel}              % Languages

% Document Info
\author{Magnus (Fylke)}
\pdfinfo{
  /Title (erlang-binaries.pdf)
  /Creator (Cheatography)
  /Author (Magnus (Fylke))
  /Subject (Erlang Binaries Cheat Sheet)
}

% Lengths and widths
\addtolength{\textwidth}{6cm}
\addtolength{\textheight}{-1cm}
\addtolength{\hoffset}{-3cm}
\addtolength{\voffset}{-2cm}
\setlength{\tabcolsep}{0.2cm} % Space between columns
\setlength{\headsep}{-12pt} % Reduce space between header and content
\setlength{\headheight}{85pt} % If less, LaTeX automatically increases it
\renewcommand{\footrulewidth}{0pt} % Remove footer line
\renewcommand{\headrulewidth}{0pt} % Remove header line
\renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit
% This two commands together give roughly
% the right line height in the tables
\renewcommand{\arraystretch}{1.3}
\onehalfspacing

% Commands
\newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour
\newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols
\newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns
\newcommand{\tn}{\tabularnewline} % Required as custom column type in use

% Font and Colours
\definecolor{HeadBackground}{HTML}{333333}
\definecolor{FootBackground}{HTML}{666666}
\definecolor{TextColor}{HTML}{333333}
\definecolor{DarkBackground}{HTML}{66AADE}
\definecolor{LightBackground}{HTML}{EBF4FA}
\renewcommand{\familydefault}{\sfdefault}
\color{TextColor}

% Header and Footer
\pagestyle{fancy}
\fancyhead{} % Set header to blank
\fancyfoot{} % Set footer to blank
\fancyhead[L]{
\noindent
\begin{multicols}{3}
\begin{tabulary}{5.8cm}{C}
    \SetRowColor{DarkBackground}
    \vspace{-7pt}
    {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent
        \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}}
    }
\end{tabulary}
\columnbreak
\begin{tabulary}{11cm}{L}
    \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Erlang Binaries Cheat Sheet}}}} \\
    \normalsize{by \textcolor{DarkBackground}{Magnus (Fylke)} via \textcolor{DarkBackground}{\uline{cheatography.com/93081/cs/20609/}}}
\end{tabulary}
\end{multicols}}

\fancyfoot[L]{ \footnotesize
\noindent
\begin{multicols}{3}
\begin{tabulary}{5.8cm}{LL}
  \SetRowColor{FootBackground}
  \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}}  \\
  \vspace{-2pt}Magnus (Fylke) \\
  \uline{cheatography.com/fylke} \\
  \end{tabulary}
\vfill
\columnbreak
\begin{tabulary}{5.8cm}{L}
  \SetRowColor{FootBackground}
  \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}}  \\
   \vspace{-2pt}Published 25th September, 2019.\\
   Updated 8th March, 2023.\\
   Page {\thepage} of \pageref{LastPage}.
\end{tabulary}
\vfill
\columnbreak
\begin{tabulary}{5.8cm}{L}
  \SetRowColor{FootBackground}
  \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}}  \\
  \SetRowColor{white}
  \vspace{-5pt}
  %\includegraphics[width=48px,height=48px]{dave.jpeg}
  Measure your website readability!\\
  www.readability-score.com
\end{tabulary}
\end{multicols}}




\begin{document}
\raggedright
\raggedcolumns

% Set font size to small. Switch to any value
% from this page to resize cheat sheet text:
% www.emerson.emory.edu/services/latex/latex_169.html
\footnotesize % Small font.

\begin{multicols*}{2}

\begin{tabularx}{8.4cm}{X}
\SetRowColor{DarkBackground}
\mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Overview}}  \tn
\SetRowColor{white}
\mymulticolumn{1}{x{8.4cm}}{Erlang can match binaries just as any list of things. `\textless{}\textless{}E1, E2, E3\textgreater{}\textgreater{} = Bin` divides the binary `Bin` into three elements of type `integer` of one byte each. This means that `Bin` has to be 24 bits long, or we get a `badmatch`. You can also make partial matches, in a `{[}Head | Tail{]}` fashion, by putting `/bitstring` on the last element, like so: `\textless{}\textless{}E1, E2, E3/bitstring\textgreater{}\textgreater{} = Bin`. This is a {\bf{type modifier}} and tells Erlang that there are two 8-bit elements, in `E1` and `E2` respectively, and then an undetermined amount of bits stored in `E3`.% Row Count 11 (+ 11)
} \tn 
\hhline{>{\arrayrulecolor{DarkBackground}}-}
\end{tabularx}
\par\addvspace{1.3em}

\begin{tabularx}{8.4cm}{x{2.66 cm} x{1.596 cm} x{3.344 cm} }
\SetRowColor{DarkBackground}
\mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Type Modifiers}}  \tn
% Row 0
\SetRowColor{LightBackground}
{\bf{Type}} & {\bf{Size in bits}} & {\bf{Remarks}} \tn 
% Row Count 2 (+ 2)
% Row 1
\SetRowColor{white}
`integer` & As many as it takes & Default size is 8 bits \tn 
% Row Count 5 (+ 3)
% Row 2
\SetRowColor{LightBackground}
`float` & {\bf{64}}|32|16 & Need to specify length if other than default: \{\{noshy\}\}`\textless{}\textless{}A:16/float\textgreater{}\textgreater{}` \tn 
% Row Count 10 (+ 5)
% Row 3
\SetRowColor{white}
\{\{noshy\}\}`binary|bytes` & 8 per chunk & Anything matched must be of size evenly divisible by 8 (this is default) \tn 
% Row Count 15 (+ 5)
% Row 4
\SetRowColor{LightBackground}
\{\{noshy\}\}`bitstring|bits` & 1 per chunk & Will always match, use as Tail for a list \tn 
% Row Count 18 (+ 3)
% Row 5
\SetRowColor{white}
\{\{noshy\}\}`utf8|utf16|utf32` & 8-32, 16-32, and 32 & `\textless{}\textless{}"abc"/utf8\textgreater{}\textgreater{}` is the same as `\textless{}\textless{}\$a/utf8, \$b/utf8, \$c/utf8\textgreater{}\textgreater{}` \tn 
% Row Count 22 (+ 4)
% Row 6
\SetRowColor{LightBackground}
\{\{noshy\}\}`signed|{\bf{unsigned}}` & N/A & Default is unsigned \tn 
% Row Count 25 (+ 3)
% Row 7
\SetRowColor{white}
\{\{noshy\}\}`{\bf{big}}|little|native` & N/A & Endianness - `native` is resolved at load time to whatever the CPU uses \tn 
% Row Count 30 (+ 5)
\end{tabularx}
\par\addvspace{1.3em}

\vfill
\columnbreak
\begin{tabularx}{8.4cm}{x{2.66 cm} x{1.596 cm} x{3.344 cm} }
\SetRowColor{DarkBackground}
\mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Type Modifiers (cont)}}  \tn
% Row 8
\SetRowColor{LightBackground}
\{\{noshy\}\}`unit:IntLiteral` & N/A & Define a custom unit of length 1..256 \tn 
% Row Count 3 (+ 3)
\hhline{>{\arrayrulecolor{DarkBackground}}---}
\end{tabularx}
\par\addvspace{1.3em}

\begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} }
\SetRowColor{DarkBackground}
\mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Examples}}  \tn
% Row 0
\SetRowColor{LightBackground}
{\bf{Expression}} & {\bf{Result}} \tn 
% Row Count 1 (+ 1)
% Row 1
\SetRowColor{white}
`\textless{}\textless{}97, 98, 99\textgreater{}\textgreater{}` & `\textless{}\textless{}"abc"\textgreater{}\textgreater{}` (turn off with \seqsplit{`shell:strings(false)`)} \tn 
% Row Count 4 (+ 3)
% Row 2
\SetRowColor{LightBackground}
`\textless{}\textless{}A:2/unit:6, B:1/unit:4\textgreater{}\textgreater{} = \textless{}\textless{}7, 42\textgreater{}\textgreater{}` & `A = 114` `B = 10` \tn 
% Row Count 6 (+ 2)
% Row 3
\SetRowColor{white}
`\textless{}\textless{}A:16/float\textgreater{}\textgreater{} = \textless{}\textless{}1, 17\textgreater{}\textgreater{}` & \seqsplit{`1.627206802368164e-5`} \tn 
% Row Count 8 (+ 2)
% Row 4
\SetRowColor{LightBackground}
`\textless{}\textless{}A/signed\textgreater{}\textgreater{} = \textless{}\textless{}255\textgreater{}\textgreater{}` & `-1` \tn 
% Row Count 10 (+ 2)
% Row 5
\SetRowColor{white}
`\textless{}\textless{}A:16/big\textgreater{}\textgreater{} = \textless{}\textless{}255, 0\textgreater{}\textgreater{}` & `65280` \tn 
% Row Count 12 (+ 2)
% Row 6
\SetRowColor{LightBackground}
`\textless{}\textless{}A:16/little\textgreater{}\textgreater{} = \textless{}\textless{}255, 0\textgreater{}\textgreater{}` & `255` \tn 
% Row Count 14 (+ 2)
% Row 7
\SetRowColor{white}
`\textless{}\textless{}"p{\"o}pc{\"o}rn"/utf8\textgreater{}\textgreater{}` & How Erlang handles unicode \tn 
% Row Count 16 (+ 2)
\hhline{>{\arrayrulecolor{DarkBackground}}--}
\SetRowColor{LightBackground}
\mymulticolumn{2}{x{8.4cm}}{When constructing a binary, if the size of an integer `N` is too large to fit inside the given segment, the most significant bits are silently discarded and only the `N` least significant bits kept.}  \tn 
\hhline{>{\arrayrulecolor{DarkBackground}}--}
\end{tabularx}
\par\addvspace{1.3em}

\begin{tabularx}{8.4cm}{X}
\SetRowColor{DarkBackground}
\mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Segments}}  \tn
\SetRowColor{white}
\mymulticolumn{1}{x{8.4cm}}{Each segment in a binary has the following general syntax: \{\{noshy\}\}`Value:Size/TypeSpecifierList`. The `Size` and `TypeSpecifier` can be omitted. \newline % Row Count 3 (+ 3)
`Value` is either a literal or a variable, `Size` is multiplied by the unit in `TypeSpecifierList`, and can be any expression that evaluates to an `integer`\textasciicircum{}1\textasciicircum{}. Think of 'Size' as the number of items of the type in the 'TypeSpecifierList' \newline % Row Count 8 (+ 5)
{\bf{Contrived example:}} `\textless{}\textless{}X:4/little-signed-integer-unit:8\textgreater{}\textgreater{}` has a total size of 4*8 = 32 bits, and it contains a signed integer in little endian byte order.% Row Count 12 (+ 4)
} \tn 
\hhline{>{\arrayrulecolor{DarkBackground}}-}
\SetRowColor{LightBackground}
\mymulticolumn{1}{x{8.4cm}}{\textasciicircum{}1\textasciicircum{} Mostly true, see Bit Syntax Expressions in Erlang documentation for complete picture.}  \tn 
\hhline{>{\arrayrulecolor{DarkBackground}}-}
\end{tabularx}
\par\addvspace{1.3em}

\begin{tabularx}{8.4cm}{X}
\SetRowColor{DarkBackground}
\mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Binary Comprehension Example}}  \tn
\SetRowColor{white}
\mymulticolumn{1}{x{8.4cm}}{Just like with lists, there is a notation for binary comprehension. Below is an example of how to use this to convert a 32 bit integer into a hex representation: \newline % Row Count 4 (+ 4)
`int\_as\_hex(Int) -\textgreater{}` \newline % Row Count 5 (+ 1)
`~~~~IntAsBin = \textless{}\textless{}Int:32\textgreater{}\textgreater{},` \newline % Row Count 6 (+ 1)
`~~~~"0x" ++ lists:flatten({[}byte\_to\_hex(\textless{}\textless{}Byte\textgreater{}\textgreater{}) || \textless{}\textless{}Byte:8\textgreater{}\textgreater{} \textless{}= IntAsBin{]}).` \newline % Row Count 8 (+ 2)
`byte\_to\_hex(\textless{}\textless{}Nibble1:4, Nibble2:4\textgreater{}\textgreater{}) -\textgreater{}` \newline % Row Count 9 (+ 1)
`~~~~{[}integer\_to\_list(Nibble1, 16), \seqsplit{integer\_to\_list(Nibble2}, 16)){]}.`% Row Count 11 (+ 2)
} \tn 
\hhline{>{\arrayrulecolor{DarkBackground}}-}
\SetRowColor{LightBackground}
\mymulticolumn{1}{x{8.4cm}}{You can mix list- and binary comprehension: if the generator is a list, use `\textless{}-`, if it's a binary, use `\textless{}=`. If you want the result to be a binary, use `\textless{}\textless{}\textgreater{}\textgreater{}`, if you want a list, use `{[}{]}` around the expression.}  \tn 
\hhline{>{\arrayrulecolor{DarkBackground}}-}
\end{tabularx}
\par\addvspace{1.3em}

\begin{tabularx}{8.4cm}{X}
\SetRowColor{DarkBackground}
\mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Troubleshooting}}  \tn
\SetRowColor{white}
\mymulticolumn{1}{x{8.4cm}}{Use the Erlang shell to trial and error you way to a correct expression. A useful tool for understanding why your binaries are badmatching is `bit\_size`: \newline % Row Count 4 (+ 4)
`bit\_size(\textless{}\textless{}1/integer\textgreater{}\textgreater{}). =\textgreater{} 8` `bit\_size(\textless{}\textless{}\textless{}\textless{}1:1, 0:1\textgreater{}\textgreater{}/bitstring\textgreater{}\textgreater{}). =\textgreater{} 2` \newline % Row Count 6 (+ 2)
`bit\_size(\textless{}\textless{}1.0/float\textgreater{}\textgreater{}). =\textgreater{} 64` `bit\_size(\textless{}\textless{}\textless{}\textless{}1, 2\textgreater{}\textgreater{}/binary\textgreater{}\textgreater{}). =\textgreater{} 16` \newline % Row Count 8 (+ 2)
A related one is `byte\_size`: \newline % Row Count 9 (+ 1)
`MinBytesToEncodeNumber = \seqsplit{byte\_size(binary:encode\_unsigned(Number))}.`% Row Count 11 (+ 2)
} \tn 
\hhline{>{\arrayrulecolor{DarkBackground}}-}
\end{tabularx}
\par\addvspace{1.3em}


% That's all folks
\end{multicols*}

\end{document}