Swift - Operators



What is an Operator in Swift?

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Or we can say that operators are special symbols that are used to perform specific operations between one or more operands.

For example, to add two numbers such as 34 and 21 we will use the + operator (34 + 21 = 55).

Type of Operators in Swift

Swift supports the following types of operators −

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Range Operators
  • Misc Operators

Let's discuss each operator separately in detail.

Swift Arithmetic Operators

Arithmetic operators are used to perform mathematic operations like addition, division, multiplication, etc. on the given operands. They always work on two operands but the type of these operands should be the same. Swift supports the following arithmetic operators −

Operator Name Example
+ Addition 20 + 30 = 50
- Subtraction 30 - 10 = 20
* Multiplication 10 * 10 = 100
/ Division 20 / 5 = 4
% Modulus 10 % 2 = 0

Swift Comparison Operators

Comparison operators are used to compare two operands and find the relationship between them. They return the result into a boolean(either true or false). Swift supports the following comparison operators −

Operator Name Example
(==) Equal 10 == 10 = true
!= Not Equal 34 != 30 = true
> Greater than 90 > 34 = true
< Less than 12 < 34 = true
>= Greater than or Equal to 30 >= 10 = true
<= Less than or Equal to 10 <= 32 = true

Swift Logical Operators

Logical operators are used to perform logical operations between the given expressions. It can also make decisions on multiple conditions. It generally works with Boolean values. Swift supports the following logical operators −

Operator Name Example
&& Logical AND X && Y
|| Logical OR X || Y
! Logical NOT !X

Swift Bitwise Operators

Bitwise operators are used to manipulate individual bits of the integer. They are commonly used to perform bit-level operations. Swift supports the following bitwise operators −

Operator Name Example
& Bitwise AND X & Y
| Bitwise OR X | Y
^ Bitwise XOR X ^ Y
~ Bitwise NOT ~X
<< Left Shift X << Y
>> Right Shift X >> Y

Swift Assignment Operators

Assignment operators are used to assign and update the value of the given variable with the new value. Swift supports the following assignment operators −

Operator Name Example
(=) Assignment X = 10
+= Assignment Add X = X + 12
-= Assignment Subtract X = X - 12
*= Assignment Multiply X = X * 12
/= Assignment Divide X = X / 12
%= Assignment Modulus X = X % 12
<<= Assignment Left Shift X = X << 12
>>= Assignment Right Shift X = X >> 12
&= Bitwise AND Assignment X = X & 12
^= Bitwise Exclusive OR Assignment X = X ^12
|= Bitwise Inclusive OR Assignment X = X | 12

Swift Misc Operators

Apart from the general operators Swift also supports some special operators and they are −

Operator Name Example
- Unary Minus -9
+ Unary Plus 2
Condition ? X : Y Ternary Conditional If Condition is true ? Then value X : Otherwise value Y

Swift Advance Operators

Apart from the basic operators Swift also provides some advanced operators that are used to manipulate complex values and they are −

  • Arithmetic Overflow Operators
  • Identity Operators
  • Identity Operators

Let's discuss each operator separately in detail.

Swift Arithmetic Overflow Operators

Arithmetic overflow operators are used to perform arithmetic operations and handle overflow very well if occurs. Or we can say that arithmetic operators work with those integers whose value may exceed the maximum or minimum bound. Swift supports the following arithmetic overflow operators −

Operator Name Example
&+ Overflow Addition Num1 &+ Num2
&- Overflow Subtraction Num1 &- Num2
&* Overflow Multiplication Num1 &* Num2

Swift Identity Operators

Identity operators are used to determine whether the given variable refers to the same instance or not. These operators work with objects and classes. They are referenced type operators. Swift supports the following type of identity operators −

Operator Name Example
+=== Identical to Value1 === Value2
!== Not Identical to Value2 !== Value2

Swift Range Operators

Range operators are used to define ranges. They are used in loops, collections and control flow statements. Swift supports the following range operators −

Operator Name Example
X…Y Closed Range 1…3 = 1, 2, 3
X..<Y Half Open Range 1..<3 = 1, 2
X… Or …X One Side Range 2… = 2, 3, 4,

Swift Operator Precedence

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, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, and those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Name Operator Precedence
Primary Expression Operators () [] . expr++ expr-- left-to-right
Unary Operators

* & + - ! ~ ++expr --expr

* / %

+ -

>> <<

< > <= >=

== !=

right-to-left
Binary Operators & ^ | && || left-to-right
Ternary Operator ?: right-to-left
Assignment Operators (= += -= *= /= %= >>= <<= &= ^= |=) right-to-left
Comma , left-to-right
Advertisements