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 Operator

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
filter_none

edit
close

play_arrow

link
brightness_4
code

# Examples of Arithmetic Operator 
a = 9
b = 4
  
println("a = ", a)
println("b = ", b)
  
# Addition of numbers 
add = a +
println("Binary Addition: ", add) 
  
# Subtraction of numbers  
sub = a -
println("Binary Subtraction: ", sub) 
  
# Multiplication of number  
mul = a *
println("Binary Multiplication: ", mul) 
  
# Division(float) of number  
div1 = a /
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 %
println("Modular Division: ", mod) 

chevron_right


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 Operator

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
filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

-49
0
115
115
12
12
192

Logical Operator

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
filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

false
true
false

Assignment Operator

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
filter_none

edit
close

play_arrow

link
brightness_4
code

# Examples of Assignment Operator 
a = 9
b = 4
  
println("a = ", a)
println("b = ", b)
  
# Addition of numbers 
a +=
println("Binary Addition: ", a) 
  
# Subtraction of numbers  
a -=
println("Binary Subtraction: ", a) 
  
# Multiplication of number  
a *=
println("Binary Multiplication: ", a) 
  
# Division(float) of number  
a /=
println("Binary Division: ", a) 
  
# Division(Integer) of number  
a ÷=
println("Integer Division: ", a) 
  
# Division(floor) of number  
a \=
println("Inverse Division: ", a) 
  
# Power of number 
a ^=
println("Power Operation: ", a) 
  
# Modulo of both number 
a %=
println("Modular Division: ", a) 

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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.
Julia-dot-operator

Relational Operator

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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) 

chevron_right


Output:

false
true
false
true
false
true



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.