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 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: (+, –, *, /)
Operator |
Description |
Syntax |
+(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 |
Python3 1==
# Examples of Arithmetic Operator
a = 9
b = 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.)
Operator |
Description |
Syntax |
~ |
Bitwise NOT |
~x |
& |
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 |
Python3 1==
# Examples of Bitwise operators
a = 48
b = 67
# Bitwise NOT operation
println(~a)
# Bitwise AND operation
println(a & b)
# Bitwise OR operation
println(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).
Operator |
Description |
Syntax |
&& |
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 |
Python3 1==
# Examples of Logical Operator
a = true
b = false
# Print if a and b both are False
println(a && b)
# Print if a or b is True
println(a || b)
# Print if not a is False
println(! 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.
Operator |
Description |
Syntax |
= |
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 |
Python3 1==
# Examples of Assignment Operator
a = 9
b = 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
Vectorized 'dot' operator
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:
Python3 1==
# Julia program to illustrate
# use of 'dot' operator
# Creating array
A = [4, 5, 6, 7]
# Performing exponent binary operation
A = A ^ 2
# Performing exponent using 'dot' operation
A = 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.
Operator |
Description |
Syntax |
> |
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 |
Example:
Python3 1==
# Examples of Relational Operators
a = 13
b = 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
Similar Reads
Vectors in Julia
Vectors in Julia are a collection of elements just like other collections like Array, Sets, Dictionaries, etc. Vector are different from Sets because vectors are ordered collections of elements, and can hold duplicate values, unlike sets which require all the elements to be unique. Vectors are one-d
5 min read
Sets in Julia
Set in Julia is a collection of elements just like other collections like arrays, dictionaries, etc. Sets are different from arrays because sets are unordered collections of unique elements whereas the order of elements is strictly remembered in Arrays. Also, Arrays and dictionaries may contain dupl
8 min read
Tuples in Julia
Tuples in Julia are an immutable collection of distinct values of same or different datatypes separated by commas. Tuples are more like arrays in Julia except that arrays only take values of similar datatypes. The values of a tuple can not be changed because tuples are immutable. Tuples are a hetero
2 min read
Operator Overloading in Julia
Operator overloading in Julia refers to the ability to define custom behavior for built-in operators such as +, -, *, /, etc. when applied to objects of custom types. This allows users to create intuitive and concise code that works with custom data types as if they were built-in. In Julia, operator
5 min read
Strings in Julia
Strings in Julia are a set of characters or a single character that is enclosed within double quotes(" "). It is a finite sequence of characters. Julia allows extracting elements of a string to form multiple substrings with the use of square brackets([ ]). Creating a String Strings in Julia can be c
6 min read
Loop Vectorization in Julia
Vectorization is used to speed up the code without using loop. Using such a function can help in minimizing the running time of code efficiently. There are two meanings of the word vectorization in the high-level languages, and they refer to different things. When we talk about vectorized code in Py
6 min read
Serialization in Julia
Like other programming languages, Julia also provides support for Serialization and De-serialization. The process of conversion of an object into byte streams (IO buffers) for the purpose of storing it into memory, file, or database is termed as Serialization. It is performed to save the object stat
2 min read
Tasks in Julia
Tasks are a control flow feature that allows computations to be suspended and resumed in a flexible manner. It is useful for IO-bound tasks, network files etc. The benefits of tasks are less memory and quick processing. It provides a function named task() to create any function as a task. The origin
3 min read
Variables in Julia
Variables are some names given to the memory location to which they are assigned. These memory locations are used to store values that can be accessed by using the name of the location, i.e. Variable. Unlike C and Java, variables in Julia need not to be written with a Datatype. Julia auto-assigns th
4 min read
Recursion in Julia
Julia is a dynamic, high-level programming language with high performance and speed that is used to perform operations in scientific computing. It is great for computational complex problems. It is an open-source language so all source code is available easily online. It is as easy to use as Python
3 min read