Arithmetic, Bitwise, Logical, Assignment, Vectorized dot, Relational
This is part 6 of the Julia Tutorials (a series of tutorials on Julia). If you are not familiar with Julia Types, please go through part 4 and part 5 for better understanding.
If you are not familiar with Julia and want to get started with it, feel free to check out the complete series @ —
Note: Use Julia’s REPL to quickly follow along with the codes in this tutorial
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc…
Operator Description Syntax
---------- --------------------------------------------- ----------
+ Plus: Adds two operands x + y
– Minus: Subtracts two operands x - y
* Multiplication: Multiplies two operands x * y
/ Division: Divides the 1st operand by 2nd x / y
^ Power: Raises x to the yth power x ^ y
% Modulus: Returns the remainder of a division x % y
Bitwise Operators
Bitwise operators are for bit level programming in which individual bits of an integer expression is manipulated.
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
Logical Operators
Logical operators are used to combine two or more conditions or to complement the evaluation of the original condition.
Operator Description Syntax
--------- --------------------------------------------- ----------
&& Logical AND: true if both operands are true x && y
|| Logical OR: true if one of operands is true x || y
! Logical NOT: true if operand is false !x
Assignment Operators
Assignment operators are used to assign values from right side operands to left side operands.
Operator Description Syntax
---------- --------------------------------------------- ----------
= Assign value from r.h.s to l.h.s x = y + z
+= a = a + b a += b
-= a = a – b a -= b
*= a = a * b a *= b
/= a = a / b a /= b
\= a = a \ b a \= b
%= a = a % b a %= b
^= a = a ^ b a ^= b
&= a = a & b a &= b
|= a = a | b a |= b
>>>= a = a >>> b a >>>= b
>>= a = a >> b a >>= b
<<= a = a << b a <<= b
Vectorized dot(.) Operator
The “dot” operator is used to perform element wise binary operations on a given array.
Syntax: Prepend dot operator to the desired binary operation.
x = x .^ 2
As you can see from this example, doing A = A ^ 2
will result in an error since it is not possible to perform square of an array, on the other hand if the dot operator is used, the squaring will be done element wise.
Relational Operators
Relational operators are used to check the relation between 2 operands.
Operator Description Syntax
---------- --------------------------- -----------------
> Greater than x > y
< Less than x < y
== Equal to x == y
!=, ≠ Not equal to x != y or x ≠ y
>=, ≥ Greater than or equal to x >= y or x ≥ y
<=, ≤ Less than or equal to x <= y or x ≤ y
Next Up — Julia Control Flow