Julia Basic Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical operations, such as:3+2=5。
The Julia language has a rich set of built-in operators. The supported operations are:
- Arithmetic Operators
- Logical Operators
- Relational Operators
- Bitwise Operators
- Assignment Operators
- Vectorized "Dot" Operators
Arithmetic Operators
The following table shows Julia's basic arithmetic operators, which apply to all basic numeric types:
| Expression | Name | Description |
|---|---|---|
+x |
Unary plus operator | Identity operation |
-x |
Unary minus operator | Turns the value into its negative |
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 |
Reverse division | Equivalent toy / x |
x ^ y |
Power operator | xofyPower |
x % y |
Remainder | Equivalent torem(x,y) |
Examples
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 Julia's Boolean operators:
| Expression | Name |
|---|---|
!x |
Negation |
x && y |
Short-circuit AND. In the expression x && y, the subexpression y is executed only if x is true. |
x || y |
Short-circuit OR. In the expression x || y, the subexpression y is executed only if x is false. |
Examples
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 Julia's relational operators:
| Operator | Name |
|---|---|
== |
Equality |
!=, ≠ |
Inequality |
< |
Less than |
<=, ≤ |
Less than or equal to |
> |
Greater than |
>=, ≥ |
Greater than or equal to |
Examples
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 especially convenient when writing numerical code. They use the && operator to compare scalars, and for arrays they use & to perform element-wise comparison. For example, 0 .< A .< 1 yields a boolean array whose elements are all true if the elements of A are all between 0 and 1.
Julia allows chained comparisons:
Examples
true
Note the evaluation order of chained comparisons:
Examples
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 Julia's bitwise operators:
| Expression | Name |
|---|---|
~x |
Bitwise NOT |
x & y |
Bitwise AND |
x | y |
Bitwise OR |
x ⊻ y |
Bitwise XOR (logical 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
-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 perform compound assignment to the left operand by placing = directly after the binary operator. For example,x += 3is equivalent to x = x + 3 。The following table shows Julia's assignment operators:
| Operator | Description | Example |
|---|---|---|
| = | Simple assignment operator: assigns the value of the right operand to the left operand. | C = A + B assigns 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 |
| %= | Modulus-and-assign operator: assigns the modulus of the two operands to the left operand. | C %= A is equivalent to C = C % A |
| <<= | Left shift and assignment operator | C <<= 2 is equivalent to C = C << 2 |
| >>= | Right shift and assignment operator | C >>= 2 is equivalent to C = C >> 2 |
| &= | Bitwise AND and assignment operator | C &= 2 is equivalent to C = C & 2 |
| ^= | Bitwise XOR and assignment operator | C ^= 2 is equivalent to C = C ^ 2 |
| |= | Bitwise OR and assignment operator | C |= 2 is equivalent to C = C | 2 |
| >>>= | Logical right shift and assignment operator | C >>>= 2 is equivalent to C = C >>>2 |
| ⊻= | XOR (logical exclusive OR) assignment operator | C ⊻= 2 is equivalent to C = C ⊻= 2 |
Examples
1
julia> x += 3
4
julia> x
4
Vectorized "Dot" Operators
In Julia, every binary operator has a corresponding "dot" operator; for example,^there is a corresponding.^exists. This corresponding.^is automatically defined by Julia to perform element-wise^operation. For example,[1,2,3] ^ 3is illegal, because mathematics has not defined the cube of an array (with different row and column lengths). But[1,2,3] .^ 3is legal in Julia, and it performs the ^ operation element-wise (also called vectorized operation), yielding [1^3, 2^3, 3^3]. Similarly,!or√unary operators like this also have a corresponding.√ used to perform element-wise operations.
Examples
3-element Vector{Int64}:
1
8
27
In addition to dot operators, we also have element-wise assignment operators, such asa .+= b(or@. a += b) is parsed asa .= a .+ b。
Operator Precedence and Associativity
Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Some operators have higher precedence than others; for example, multiplication and division operators have higher precedence than addition and subtraction operators.
For examplex = 7 + 3 * 2, here, x is assigned 13 instead of 20, because the operator*has+higher precedence, so the multiplication 3*2 is computed first, and then added to 7.
The following table lists operators by precedence from highest to lowest. Operators with higher precedence appear at the top of the table, and operators with lower precedence appear at the bottom. In an expression, operators with higher precedence are evaluated first.
| Category | Operator | Associativity |
|---|---|---|
| Syntax | . followed by :: |
Left-associative |
| Exponentiation | ^ |
Right-associative |
| Unary operators | + - √ |
Right-associative |
| Shift operations | << >> >>> |
Left-associative |
| Division | // |
Left-associative |
| Multiplication | * / % & \ ÷ |
Left-associative |
| Addition | + - | ⊻ |
Left-associative |
| Syntax | : .. |
Left-associative |
| Syntax | |> |
Left-associative |
| Syntax | <| |
Right-associative |
| Comparison | > < >= <= == === != !== <: |
Non-associative |
| Flow control | && followed by || followed by ? |
Right-associative |
| Pair operations | => |
Right-associative |
| Assignment | = += -= *= /= //= \= ^= ÷= %= |= &= ⊻= <<= >>= >>>= |
Right-associative |
We can also use the built-in functionBase.operator_precedenceto view the precedence value of any given operator; the larger the value, the higher the precedence.
Examples
(11, 12, 17)
julia> Base.operator_precedence(:sin), Base.operator_precedence(:+=), Base.operator_precedence(:(=)) # (Note: there must be parentheses around the equals sign `:(=)`)
(0, 1, 1)
Additionally, built-in functionsBase.operator_associativitycan return a symbolic representation of operator associativity:
Examples
(:left, :none, :right)
julia> Base.operator_associativity(:⊗), Base.operator_associativity(:sin), Base.operator_associativity(:→)
(:left, :none, :right)