Binary Data In Elixir — Tentamen Software Testing Blog

Image for post
Image for post
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.

Written by

Founder of Tentamen, software testing agency.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store