YouTip LogoYouTip

Julia Basic Operators

Julia Basic Operators |

Operators are symbols that tell the compiler to perform specific mathematical or logical operations, such as: 3+2=5.

Julia language has built-in rich operators, supporting operations such as:

  • Arithmetic operators
  • Logical operators
  • Relational operators
  • Bitwise operators
  • Assignment operators
  • Vectorized "dot" operators

Arithmetic Operators

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

Expression Name Description
+x Unary Plus Operator Identity operation
-x Unary Minus Operator Negates the value
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 Returns the integer part of x / y
x y Backslash Division Equivalent to y / x
x ^ y Power Operator x raised to the power of y
x % y Modulo Operator 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 following table shows the Boolean operators in Julia:

Expression Name
!x Not
x && y Short-Circuit And, in expression x && y, sub-expression y is only evaluated when x is true.
x || y Short-Circuit Or, in expression x || y, sub-expression y is only evaluated when 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 following table shows the relational operators in Julia:

Operator Name
== Equal
!=, β‰  Not Equal
< 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 scalars and & for arrays element-wise comparison. For example, 0 .< A .< 1 returns a boolean array where each element is true if the corresponding element in A is 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 following table 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
x ⊽ y Bitwise NOR
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

Every binary operator and bitwise operator can be used for compound assignment by placing = directly after the binary operator. For example, x += 3 is equivalent to x = x + 3.

The following table shows the assignment operators in Julia:

Operator Description Example
= Simple Assignment Operator, assigns the value of the right operand to the left operand C = A + B will assign the value of A + B to C
+= Add and Assign Operator, assigns the result of adding the right operand to the left operand to the left operand C += A is equivalent to C = C + A
-= Subtract and Assign Operator, assigns the result of subtracting the right operand from the left operand to the left operand C -= A is equivalent to C = C - A
*= Multiply and Assign Operator, assigns the result of multiplying the right operand by the left operand to the left operand C *= A is equivalent to C = C * A
/= Divide and Assign Operator, assigns the result of dividing the left operand by the right operand to the left operand C /= A is equivalent to C = C / A
%= Modulo and Assign Operator, assigns the modulo of the two operands to the left operand C %= A is equivalent to C = C % A
<<= Left Shift and Assign Operator C <<= 2 is equivalent to C = C << 2
>>= Right Shift and Assign Operator C >>= 2 is equivalent to C = C >> 2
&= Bitwise AND and Assign Operator C &= 2 is equivalent to C = C & 2
^= Bitwise XOR and Assign Operator C ^= 2 is equivalent to C = C ^ 2
|= Bitwise OR and Assign Operator C |= 2 is equivalent to C = C | 2
>>>= Logical Right Shift and Assign Operator C >>>= 2 is equivalent to C = C >>> 2
⊻= XOR (Logical XOR) Assign Operator C ⊻= 2 is equivalent to C = C ⊻= 2

Examples

julia> x = 1

1

julia> x += 3

4

julia> x

4

Vectorized "Dot" Operators

In Julia, every binary operator has a corresponding "dot" operator. For example, ^ has a corresponding .^. This corresponding .^ is automatically defined in Julia to perform element-wise ^ operations. For instance, [1,2,3] ^ 3 is invalid because there's no mathematical definition for the cube of (unequal-sized) arrays. However, [1,2,3] .^ 3 is valid in Julia, performing element-wise ^ operations (also known as vectorized operations), resulting in [1^3, 2^3, 3^3]. Similarly, unary operators like ! or √ also have corresponding .√ for element-wise operations.

Examples

julia>[1,2,3] .^ 3

3-element Vector{Int64}:

1

8

27

Aside from dot operators, we also have element-wise assignment operators. For example, a .+= b (or @. a += b) is parsed as a .= a .+ b.


Operator Precedence and Associativity

Operator precedence determines how terms in an expression are grouped. It affects how an expression is calculated. Some operators have higher precedence than others. For example, multiplication and division operators have higher precedence than addition and subtraction operators.

For example, in x = 7 + 3 * 2, x is assigned the value of 13, not 20, because the * operator has higher precedence than +, so the multiplication 3*2 is computed first, then added to 7.

The following table lists all operators in descending order of precedence. Operators with higher precedence appear at the top of the table, while those with lower precedence appear at the bottom. In expressions, operators with higher precedence are computed first.

Category Operators Associativity
Syntax . followed by :: Left associative
Power ^ Right associative
Unary Operators + - √ Right associative
Shift Operators << >> >>> Left associative
Division // Left associative
Multiplication * / % & Γ· Left associative
Addition + - | ⊻ Left associative
Syntax : .. Left associative
Syntax |> Left associative
Syntax <| Right associative
Comparison > < >= <= == === != !== <: No associativity
Control Flow && followed by || followed by ? Right associative
Pair Operations => Right associative
Assignment = += -= *= /= //= = ^= ÷= %= |= &= ⊻= <<= >>= >>>= Right associative

We can also check the precedence value of any given operator using the built-in function Base.operator_precedence; the higher the number, the higher the precedence:

Examples

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 required around equals sign `:(=)`)

(0, 1, 1)

Additionally, the built-in function Base.operator_associativity returns the symbol representation of an operator's associativity:

Examples

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 StringsJulia Data Type β†’