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.
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.
Elixir is using special operators for representing binaries:
<< >>. We can use numbers from
255 because each number represents a
255 = 1 1 1 1 1 1 1 1
In the picture above, we first defined three binary number of two
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
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.
- what is a binary type of
- how to represent binary using
- the default number of bytes and how to set a number of byte