Operators in Julia
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’.
Types of Operators
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: (+, –, *, /)
|+(unary plus)||Unary plus: Identity operation||+x|
|-(unary minus)||Unary minus: Performs negation on operand||-x|
|+||Binary plus: adds two operands||x + y|
|–||Binary minus: subtracts two operands||x – y|
|*||Multiplication(times): multiplies two operands||x * y|
|/||Division (float): divides the first operand by the second and returns float value||x / y|
|÷||Division (Integer): divides the first operand by the second and returns integer value||x ÷ y|
|\||Division (Inverse): divides the second operand by the first(y/x)||x \ y|
|^||Power: Raises x to the yth power||x ^ y|
|%||Modulus: returns the remainder when first operand is divided by the second||x % y|
|!||Negation: Changes bool value i.e. from true to false and vice versa||x % y|
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.)
|&||Bitwise AND||x & y|
||||Bitwise OR||x | y|
|⊻||Bitwise XOR||x ⊻ y|
|>>>||Logical right shift||x >>> y|
|>>||Bitwise right shift||x >> y|
|<<||Bitwise/Logical left shift||x << y|
-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).
|&&||Logical AND: True if both the operands are true||x && y|
|||||Logical OR: True if either of the operands is true||x || y|
|!||Logical NOT: True if operand is false||!x|
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.
|=||Assign value of right side of expression to left side operand||x = y + z|
|+=||Add AND: Add right side operand with left side operand and then assign to left operand||a += b a = a + b|
|-=||Subtract AND: Subtract right operand from left operand and then assign to left operand||a -= b a = a – b|
|*=||Multiply AND: Multiply right operand with left operand and then assign to left operand||a *= b a = a * b|
|/=||Divide AND: Divide left operand with right operand and then assign to left operand||a /= b a = a / b|
|\=||Inverse Divide AND: Divide right operand with left operand and then assign to left operand||a \= b a = a \ b|
|÷=||Integer Divide AND: Divide left operand with right operand and then assign to left operand||a ÷= b a = a ÷ b|
|%=||Modulus AND: Takes modulus using left and right operands and assign result to left operand||a %= b a = a % b|
|^=||Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand||a ^= b a = a ^ b|
|&=||Performs Bitwise AND on operands and assign value to left operand||a &= b a = a & b|
||=||Performs Bitwise OR on operands and assign value to left operand||a |= b a = a | b|
|⊻=||Performs Bitwise xOR on operands and assign value to left operand||a ⊻= b a = a ⊻ b|
|>>>=||Performs Logical right shift on operands and assign value to left operand||a>>>=b a=a>>>b||>>=||Performs Bitwise right shift on operands and assign value to left operand||a >>= b a = a >> b|
|<<=||Performs Bitwise left shift on operands and assign value to left operand||a <<= b a = a << b|
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.
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.
|>||Greater than: True if left operand is greater than the right||x > y|
|<||Less than: True if left operand is less than the right||x < y|
|==||Equal to: True if both operands are equal||x == y|
|!=, ≠||Not equal to – True if operands are not equal||x != y or x ≠ y|
|>=, ≥||Greater than or equal to: True if left operand is greater than or equal to the right||x >= y or x ≥ y|
|<=, ≤||Less than or equal to: True if left operand is less than or equal to the right||x <= y or x ≤ y|
false true false true false true