Easy Tutorial
❮ Julia Data Type Home ❯

Julia Basic Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical operations, such as 3+2=5.

Julia language comes with a rich set of built-in operators, supporting operations such as:


Arithmetic Operators

The table below shows the basic arithmetic operators in Julia, which are applicable to all basic numeric types:

Expression Name Description
+x Unary plus operator Identity operation
-x Unary minus operator Changes the value to its opposite
x + y Binary addition operator Adds two numbers
x - y Binary subtraction operator Subtracts two numbers
x * y Multiplication operator Multiplies two numbers
x / y Division operator Divides two numbers
x ÷ y Integer division Takes the integer part of x / y
x \ y Backslash division Equivalent to y / x
x ^ y Exponentiation operator x to the power of y
x % y Remainder Equivalent to rem(x, y)

Examples

julia> 1 + 2 + 3
6
julia> 1 - 2
-1
julia> 3*2/12
0.5
julia> 2+20-5
17
julia> 50*2/10
10.0
julia> 23%2
1
julia> 2^4
16

Boolean Operators

The table below shows the Boolean operators in Julia:

Expression Name
!x Negation
x && y Short-circuit AND, subexpression y is only evaluated if x is true
x y Short-circuit OR, subexpression y is only evaluated if x is false

Examples

julia> !true
false
julia> !false
true
julia> true && (x = (1, 2, 3))
(1, 2, 3)
julia> false && (x = (1, 2, 3))
false
julia> false || (x = (1, 2, 3))
(1, 2, 3)

Relational Operators

The table below shows the relational operators in Julia:

Operator Name
== Equality
!=, ≠ Inequality
< Less than
<=, ≤ Less than or equal to
> Greater than
>=, ≥ Greater than or equal to

Examples

julia> 100 == 100
true
julia> 100 == 101
false
julia> 100 != 101
true
julia> 100 == 100.0
true
julia> 100 < 500
true
julia> 100 > 500
false
julia> 100 >= 100.0
true
julia> -100 <= 100
true
julia> -100 <= -100
true
julia> -100 <= -500
false
julia> 100 < -10.0
false

Chained Comparisons

Chained comparisons are particularly convenient when writing numerical code. They use the && operator for scalar comparisons and & for element-wise comparisons in arrays. For example, 0 .< A .< 1 will yield a boolean array where the elements are true if the corresponding elements of A are between 0 and 1.

Julia allows chained comparisons:

Examples

julia> 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
true

Note the execution order of chained comparisons:

Examples

julia> M(a) = (println(a); a)
M (generic function with 1 method)
julia> M(1) < M(2) <= M(3)
2
1
3
true
julia> M(1) > M(2) <= M(3)
2
1
false

Bitwise Operators

The table below shows the bitwise operators in Julia:

Expression Name
~x Bitwise NOT
x & y Bitwise AND
x y Bitwise OR
x ⊻ y Bitwise XOR (exclusive OR)
x ⊼ y Bitwise NAND (not AND)
x ⊽ y Bitwise NOR (not OR)
x >>> y Logical right shift
x >> y Arithmetic right shift
x << y Logical/arithmetic left shift

Examples

julia> ~123
-124
julia> 123 & 234
106
julia> 123 | 234
251
julia> 123 ⊻ 234
145
julia> xor(123, 234)
145
julia> nand(123, 123)
-124

julia> 123 ⊼ 123
-124

julia> nor(123, 124)
-128

julia> 123 ⊽ 124
-128

julia> ~UInt32(123)
0xffffff84

julia> ~UInt8(123)
0x84

Assignment Operators

The assignment operator x += 3 is equivalent to x = x + 3.

The table below shows the assignment operators in Julia:

Operator Description Example
= Simple assignment operator, assigns values from right side operands to left side operand C = A + B will assign the value of A + B to C
+= Add AND assignment operator, adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
-= Subtract AND assignment operator, subtracts right operand from the left operand and assigns the result to left operand C -= A is equivalent to C = C - A
*= Multiply AND assignment operator, multiplies right operand with the left operand and assigns the result to left operand C *= A is equivalent to C = C * A
/= Divide AND assignment operator, divides left operand with the right operand and assigns the result to left operand C /= A is equivalent to C = C / A
%= Modulus AND assignment operator, takes modulus using two operands and assigns the result to left operand C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator C <<= 2 is the same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is the same as C = C >> 2
&= Bitwise AND assignment operator C &= 2 is the same as C = C & 2
^= Bitwise XOR assignment operator C ^= 2 is the same as C = C ^ 2
= Bitwise OR assignment operator C = 2 is the same as C = C 2
>>>= Logical left shift operator C >>>= 2 is the same as C = C >>> 2
⊻= XOR (logical XOR) assignment operator C ⊻= 2 is the same as C = C ⊻= 2

Example

julia> x = 1
1

julia> x += 3
4

julia> x
4

Vectorized "Dot" Operators

In Julia, each binary operator has a corresponding "dot" operator, such as ^ having .^. This .^ is automatically defined to perform element-wise ^ operations. For example, [1,2,3] ^ 3 is illegal because there is no mathematical definition for the cube of arrays with different dimensions. However, [1,2,3] .^ 3 is legal in Julia and will perform an element-wise ^ operation, resulting in [1^3, 2^3, 3^3]. Similarly, unary operators like ! or have a corresponding .√ for element-wise operations.

Example

julia> [1,2,3] .^ 3
3-element Vector{Int64}:
  1
  8
 27

In addition to dot operators, we have pointwise assignment operators, such as a .+= b (or @. a += b), which is parsed as a .= a .+ b.


Operator Precedence and Associativity

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator.

For example, in x = 7 + 3 * 2, x is assigned 13, not 20, because the * operator has higher precedence than the + operator, so it first multiplies 3 by 2, then adds 7.

The following table lists the operators in order of precedence from highest to lowest. Operators with higher precedence appear at the top of the table, and those with lower precedence at the bottom. In an expression, higher precedence operators will be evaluated first.

Category Operator Associativity
Syntax . followed by :: Left
Exponentiation ^ Right
Unary + - √ Right
Bitshift << >> >>> Left
Division // Left
Multiplication * / % & \ ÷ Left
Addition + - Left
Syntax : .. Left
Syntax > Left
Syntax < Right
Comparison > < >= <= == === != !== <: None
Control flow && followed by followed by ? Right
Pair operation => Right
Assignment Operators | = += -= *= /= //= \= ^= ÷= %= |= &= ⊻= <<= >>= >>>= | Right Associative |

We can also use the built-in function `Base.operator_precedence` to check the precedence value of any given operator, with higher values indicating higher precedence:

## Example

```julia
julia> Base.operator_precedence(:+), Base.operator_precedence(:*), Base.operator_precedence(:.)
(11, 12, 17)

julia> Base.operator_precedence(:sin), Base.operator_precedence(:+=), Base.operator_precedence(:(=))  # (Note: parentheses must surround the equals sign `:(=)`)
(0, 1, 1)

Additionally, the built-in function Base.operator_associativity can return the symbolic representation of an operator's associativity:

Example

julia> Base.operator_associativity(:-), Base.operator_associativity(:+), Base.operator_associativity(:^)
(:left, :none, :right)

julia> Base.operator_associativity(:⊗), Base.operator_associativity(:sin), Base.operator_associativity(:→)
(:left, :none, :right)
❮ Julia Data Type Home ❯