Operators in Julia

09 Mar, 2020
Operators in Julia are the mathematical symbols that are used to perform operations on variables and values. These symbols are used to carry out arithmetic and logical computations. Variables on which the operators perform operations are termed as Operands. In other words, we can say that an operator operates the operands.For example, consider the below statement:
c = a + b;
Here, ‘+’ is the operator known as the addition operator and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.
Operators in Julia are of Six types:
Arithmetic Operators
Bitwise Operators
Logical Operators
Assignment Operators
Vectorized ‘dot’ Operators
Relational Operators
Arithmetic operators are used to perform arithmetic/mathematical operations on operands. These operators include the process of addition, subtraction, multiplication, division, etc. Examples: (+, -, *, /, %, +x, -x).Arithmetic operators are of two types:
Unary Operators: Operators that operate or work with a single operand are unary operators. For example: (+x, -x) i.e. unary plus and unary minus.
Binary Operators: Operators that operate or work with two operands are binary operators. For example: (+, –, *, /)
# Examples of Arithmetic Operator a = 9b = 4  println("a = ", a)println("b = ", b)  # Addition of numbers add = a + b println("Binary Addition: ", add)   # Subtraction of numbers  sub = a - b println("Binary Subtraction: ", sub)   # Multiplication of number  mul = a * b println("Binary Multiplication: ", mul)   # Division(float) of number  div1 = a / b println("Binary Division: ", div1)   # Division(Integer) of number  div2 = a ÷ b println("Integer Division: ", div2)   # Division(floor) of number  div3 = a \ b println("Inverse Division: ", div3)   # Power of number pow = a ^ b println("Power Operation: ", pow)   # Modulo of both number mod = a % b println("Modular Division: ", mod) 
Output:
a = 9
b = 4
Binary Addition: 13
Binary Subtraction: 5
Binary Multiplication: 36
Binary Division: 2.25
Integer Division: 2
Inverse Division: 0.4444444444444444
Power Operation: 6561
Modular Division: 1
Bitwise operators are used to perform manipulation of individual bits of a number. They can be used with any of the integer types. Examples: (~, &, |, >>, <<, etc.)
# Examples of Bitwise operatorsa = 48b = 67  # Bitwise NOT operation println(~a)  # Bitwise AND operation  println(a & b)  # Bitwise OR operationprintln(a | b)  # Bitwise XOR operation println(a ? b)  # Logical right shift operation println(a >>> 2)  # Bitwise right shift operation println(a >> 2)  # Bitwise left shift operation println(a << 2)
Output:
-49
0
115
115
12
12
192

Logical Operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition into consideration. The result of the operation of a logical operator is a boolean value either true or false. For example, the logical AND represented as ‘&&’ operator in Julia returns true when both the conditions under consideration are satisfied. Otherwise, it returns false. Therefore, a && b returns true when both a and b are true (i.e. non-zero).
# Examples of Logical Operatora = trueb = false  # Print if a and b both are Falseprintln(a && b)  # Print if a or b is Trueprintln(a || b)  # Print if not a is Falseprintln(! a)
Output:
false
true
false

Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.
# Examples of Assignment Operator a = 9b = 4  println("a = ", a)println("b = ", b)  # Addition of numbers a += b println("Binary Addition: ", a)   # Subtraction of numbers  a -= b println("Binary Subtraction: ", a)   # Multiplication of number  a *= b println("Binary Multiplication: ", a)   # Division(float) of number  a /= b println("Binary Division: ", a)   # Division(Integer) of number  a ÷= b println("Integer Division: ", a)   # Division(floor) of number  a \= b println("Inverse Division: ", a)   # Power of number a ^= b println("Power Operation: ", a)   # Modulo of both number a %= b println("Modular Division: ", a) 
Output
a = 9
b = 4
Binary Addition: 13
Binary Subtraction: 9
Binary Multiplication: 36
Binary Division: 9.0
Integer Division: 2.0
Inverse Division: 2.0
Power Operation: 16.0
Modular Division: 0.0

A ‘dot’ operator(.) is used to perform a binary operation with which it is used on the entire array, element by element, one by one. For ex- A power(^) operator if applied on an array like [4, 5, 6, 7] ^ 2, will result in an error, because it is not possible to perform ‘square’ of an array. Hence, the ‘dot’ operator comes into use. When used with the binary operation like .^ it will perform the operation on each element of the array. For ex- [4, 5, 6, 7] ^ 2 will result in [4^2, 5^2, 6^2, 7^2].Similarly, this dot operator can be used with other binary operators like .=, .+, .-, etc.
Example:
# Julia program to illustrate# use of 'dot' operator  # Creating arrayA = [4, 5, 6, 7]  # Performing exponent binary operationA = A ^ 2  # Performing exponent using 'dot' operationA = A .^ 2 println(A)
Above code will generate an error when the exponent is performed without a ‘dot’ operator. This is because exponent can’t be performed on the array of elements.
These operators are used to check for relations like equality, greater than, less than. They return boolean result after the comparison and are extensively used in looping statements as well as conditional if-else statements.
Example:
# Examples of Relational Operators a = 13b = 33    # a > b is False println(a > b)     # a < b is True println(a < b)     # a == b is False println(a == b)     # a != b is True println(a != b)     # a >= b is False println(a >= b)     # a <= b is True println(a <= b) 
Output:
false
true
false
true
false
true

Julia-Basics
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Vectors in Julia
Getting rounded value of a number in Julia - round() Method
Storing Output on a File in Julia
Reshaping array dimensions in Julia | Array reshape() Method
Manipulating matrices in Julia
Exception handling in Julia
Tuples in Julia
while loop in Julia
Comments in Julia
Creating array with repeated elements in Julia - repeat() Method