Elixir : Basics of Integer datatype

Arunmuthuram M
3 min readAug 25, 2023

--

A datatype in a programming language defines what kind of values it can hold and what type of operations that can be performed on it. Elixir, like other languages, provides built-in datatypes such as integer, float, atom, list, tuple etc. This article will be on the basics of Integer datatype.

a = 123 
b = -12234234234

The integer implicitly means a signed int that supports both positive and negative whole numbers. When it comes to creating an integer and binding it to a variable, there is not much to talk about except the fact that the operator = is not an assignment operator like other languages, but a match operator. But for now, we will focus on the integer datatype. So now the question would be what is the minimum and maximum value that the integer type can hold? Well, the answer is, as much as it can. Elixir follows Arbitrary-precision arithmetic which means that the largest and the smallest integers that can be created, would depend on the available memory of the system. This eliminates multiple integer data types, each with different size restrictions, like in languages such as Java and c#.

Some other cool things about integers in elixir is that you can use an underscore to increase the readability of integers. This doesn’t affect how the integer is stored internally in any way. You can also directly use binary, octal and hexadecimal representation of numbers using the 0b , 0o and 0x notations, followed by the digits in their respective base systems.

a = 123_231_231_23 # 12323123123
b = 0b1010 # 10 (decimal equivalent)
c = 0o17 # 15
d = 0x1a # 26

Operators

Integers in elixir support arithmetic operators +, -, * and /. The division operator always returns a float. You can use the Kernel’s div/2 and rem/2 functions to get integer quotient and integer remainder. Elixir also provides the exponentiation operator ** .

5 + 5 # 10
2 - 3 # -1
4 * 5 # 20
3 / 2 # 1.5
div(3,2) # 1
rem(3,2) # 1
2 ** 3 # 8

In addition to the arithmetic operators, elixir supports comparison operators such as ==: Equal to, ===: Strictly equal to, !=: Not equal to, !==: Strictly not equal to, <: Less than, >: Greater than, <=: Less than or equal to and >=: Greater than or equal to.

5 == 5 # true
5 === 5.0 # false
4 != 4.0 # false
4 !== 4.0 # true
4 < 5 # true
4 > 5 # false
4 >= 4 # true
4 <= 3 # false

Elixir also has the concept of truthy and falsy. The values false and nil are considered falsy and anything other than these are considered truthy. The logical operators and, or and not are supported, provided the first operand should not be a non-boolean. You can use the equivalent && , || and ! operators if the first operand is a non-boolean, which includes the integers. Note that these logical operators are short-circuit operators, meaning that they only evaluate the right side if the left side evaluation is not enough to determine the result.

1 and true
** (BadBooleanError) expected a boolean on left-side of "and", got: 1
not 5
** (ArgumentError) argument error
true and 1 # 1(truthy)
false or 5 # 5(truthy)
1 && true # true
1 && 2 # 2 (truthy)
1 || nil # 1 (truthy)
2 || 3 # 2 (truthy)
!4 # false

And finally you have the bitwise operators &&&: Bitwise AND, |||: Bitwise OR, <<<: Bitwise shift left and >>>: Bitwise shift right, which is available for use after importing the Bitwise module.

Reference modules

In elixir, all of the data types have their own reference module of the same name which consists of functions that can operate on the respective data type. The Integer module for the integer data type consists of many useful functions. Other modules such as the Kernel and Erlang’s :math module also contain most frequently used utility functions that operate on integers. If you are curious about the internal representation of integers in elixir, check out these articles.

Internal representation of small integers.

Internal representation of big integers.

--

--

No responses yet