Binary Data In Elixir — Tentamen Software Testing Blog

Karlo Smid
2 min readAug 27, 2020
Elixir Binary Data

TL;DR

In the previous post, we explained how to use maps in Elixir. Today we discuss how Elixir handles fundamental computer datatype, binaries. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.

Binaries

Every program must be translated into a stream of zeros and ones because the processor only understands two states (currency levels). Stream of zeros and ones is better known as binary data. For example, number five in binary data is:

5 = 101.

We need three binary digits to represent number five. You can read more about binaries in our previous post, Binary Numbers and Binary Numbers Arithmetic.

Elixir is using special operators for representing binaries: << >>. We can use numbers from -255 to 255 because each number represents a byte and byte has eight bits.

255 = 1 1 1 1 1 1 1 1

In the picture above, we first defined three binary number of two bytes.

When we try to use a number higher than 255, Elixir automatically subtracts 256 from that number without any error. Be aware of that because this could be a source of software errors.

We can also use -1. In this case, 256 is automatically added.

It is also possible to override size of one byte for each number separately.

Originally published at https://blog.tentamen.eu on August 27, 2020.

--

--