- Elixir Tutorial
- Elixir - Home
- Elixir - Overview
- Elixir - Environment
- Elixir - Basic Syntax
- Elixir - Data Types
- Elixir - Variables
- Elixir - Operators
- Elixir - Pattern Matching
- Elixir - Decision Making
- Elixir - Strings
- Elixir - Char Lists
- Elixir - Lists and Tuples
- Elixir - Keyword Lists
- Elixir - Maps
- Elixir - Modules
- Elixir - Aliases
- Elixir - Functions
- Elixir - Recursion
- Elixir - Loops
- Elixir - Enumerables
- Elixir - Streams
- Elixir - Structs
- Elixir - Protocols
- Elixir - File I/O
- Elixir - Processes
- Elixir - Sigils
- Elixir - Comprehensions
- Elixir - Typespecs
- Elixir - Behaviours
- Elixir - Errors Handling
- Elixir - Macros
- Elixir - Libraries

- Elixir Useful Resources
- Elixir - Quick Guide
- Elixir - Useful Resources
- Elixir - Discussion

# Elixir - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. There are a LOT of operators provided by elixir. They are divided in the following categories −

- Arithmetic operators
- Comparison operators
- Boolean operators
- Misc operators

## Arithmetic Operators

The following table shows all the arithmetic operators supported by Elixir language. Assume variable **A** holds 10 and variable **B** holds 20, then −

Operator | Description | Example |
---|---|---|

+ | Adds 2 numbers. | A + B will give 30 |

- | Subtracts second number from first. | A-B will give -10 |

* | Multiplies two numbers. | A*B will give 200 |

/ | Divides first number from second. This casts the numbers in floats and gives a float result | A/B will give 0.5. |

div | This function is used to get the quotient on division. | div(10,20) will give 0 |

rem | This function is used to get the remainder on division. | rem(A, B) will give 10 |

## Comparison Operators

The comparison operators in Elixir are mostly common to those provided in most other languages. The following table sums up comparison operators in Elixir. Assume variable **A** holds 10 and variable **B** holds 20, then −

Operator | Description | Example |
---|---|---|

== | Checks if value on left is equal to value on right(Type casts values if they are not the same type). | A == B will give false |

!= | Checks if value on left is not equal to value on right. | A != B will give true |

=== | Checks if type of value on left equals type of value on right, if yes then check the same for value. | A === B will give false |

!== | Same as above but checks for inequality instead of equality. | A !== B will give true |

> | Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true. | A > B will give false |

< | Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true. | A < B will give true |

>= | Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true. | A >= B will give false |

<= | Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true. | A <= B will give true |

## Logical operators

Elixir provides 6 logical operators: and, or, not, &&, || and !. The first three, **and or not** are strict Boolean operators, meaning that they expect their first argument to be a Boolean. Non Boolean argument will raise an error. While the next three, **&&, || and !** are non strict, do not require us to have the first value strictly as a boolean. They work in the same way as their strict counterparts. Assume variable **A** holds true and variable **B** holds 20, then −

Operator | Description | Example |
---|---|---|

and | Checks if both values provided are truthy, if yes then returns the value of second variable. (Logical and). | A and B will give 20 |

or | Checks if either value provided is truthy. Returns whichever value is truthy. Else returns false. (Logical or). | A or B will give true |

not | Unary operator which inverts the value of given input. | not A will give false |

&& | Non-strict and. Works same as and but does not expect first argument to be a Boolean. |
B && A will give 20 |

|| | Non-strict or. Works same as or but does not expect first argument to be a Boolean. |
B || A will give true |

! | Non-strict not. Works same as not but does not expect the argument to be a Boolean. |
!A will give false |

**NOTE −***and*, *or*, *&&* and *||* || are short circuit operators. This means that if the first argument of **and** is false, then it will not further check for the second one. And if the first argument of **or** is true, then it will not check for the second one. For example,

false and raise("An error") #This won't raise an error as raise function wont get executed because of short #circuiting nature of and operator

## Bitwise Operators

Bitwise operators work on bits and perform bit by bit operation. Elixir provides bitwise modules as part of the package **Bitwise**, so in order to use these, you need to *use* the bitwise module. To use it, enter the following command in your shell −

use Bitwise

Assume A to be 5 and B to be 6 for the following examples −

Operator | Description | Example |
---|---|---|

&&& | Bitwise and operator copies a bit to result if it exists in both operands. | A &&& B will give 4 |

||| | Bitwise or operator copies a bit to result if it exists in either operand. | A ||| B will give 7 |

>>> | Bitwise right shift operator shifts first operand bits to the right by the number specified in second operand. | A >>> B will give 0 |

<<< | Bitwise left shift operator shifts first operand bits to the left by the number specified in second operand. | A <<< B will give 320 |

^^^ | Bitwise XOR operator copies a bit to result only if it is different on both operands. | A ^^^ B will give 3 |

~~~ | Unary bitwise not inverts the bits on the given number. | ~~~A will give -6 |

## Misc Operators

Other than the above operators, Elixir also provides a range of other operators like ** Concatenation Operator, Match Operator, Pin Operator, Pipe Operator, String Match Operator, Code Point Operator, Capture Operator, Ternary Operator** that make it quite a powerful language.