Introduction to Swift Operators
An operator is a symbol that helps to perform various logical and mathematical computations. Swift supports most of the standard C operators.
They are classified into three types:
 Unary operators: These operators operate on a single operand only. For example, increment operator.
 Binary operators: These operators operate on two operands. For example, the addition operator.
 Ternary operators: These operators operate on three operands. For example, a ? b: c.
Swift Operators
Different operators present in swift are as follows:
 Arithmetic Operator
 Logical Operator
 Assignment Operator
 Comparison Operator
 Bitwise Operator
 Range Operator
 Miscellaneous Operator
Now let us see each type of operator in detail:
1. Swift Arithmetic Operators
These operators are used to perform mathematical calculations on the operands.
Operator 
Symbol  Explanation 
Format 
Addition 
+ 
Adds given two operands 
x + y 
Subtraction 
– 
Subtracts the right operand from the left one. 
x – y 
Multiplication 
* 
Multiplies two operands 
x * y

Division 
/ 
Divides the numerator by the denominator 
x / y

Modulus 
% 
Returns the remainder after performing division 
x % y 
Example:
print(5 + 2)
print(5  2)
print(5 * 2)
print(5 / 2)
print(5 % 2)
Output:
7
3
10
2
1
2. Swift Logical Operator
These operators return Boolean values taking Boolean values as input.
Operator  Symbol  Explanation  Format 
Logical AND  &&  Returns true if all expressions are true; else return false  x && y 
Logical OR    Returns false if all expressions are false; else return true  x  y 
Logical NOT  !  Inverses the input, i.e. return true for false and vice versa  !x 
Example:
print(true && true)
print(true && false)
print(true  false)
print(false  false)
print(! false)
Output:
true
false
true
false
true
3. Swift Assignment Operator
These operators are used to assign values to a variable.
Operator  Symbol  Explanation  Format 
Assignment  =  Assigns a value of right operand to the left operand  x = y 
Addition  +=  Adds two operands and then assigns a value to the left operand  x += y 
Subtraction  =  Subtracts right operand from left operand and then assigns the value to left operand  x = y 
Multiplication  *=  Multiplies two operands and then assigns a value to the left operand  x *= y 
Division  /=  Divides the numerator by denominator and then assigns a value to the left operand  x /= y 
Modulus  %=  Returns remainder after division and then assigns a value to left operand  x %= y 
Bitwise AND  &=  Compares the binary value of two operands, return 1 if both operands are 1 else return 0 and assign a value to the left operand  x &= y 
Bitwise OR  =  Compares the binary value of two operands, return 0 if both operands are 0 else return 1 and assign the value to the left operand  x = y 
Bitwise XOR  ^=  Compares the binary value of two operands, return 0 if both operands are same else return 1 and assign a value to the left operand  x ^= y 
Left Shift  <<=  Shifts the bits towards the left and assign the result to the left operand  x <<= 2 
Right Shift  >>=  Shifts the bits towards the right and assign the result to the left operand  x >>= 2 
Example:
let a = 5
print (a)
var x = 6
print(x += 3)
print(x = 3)
print(x *= 3)
print(x /= 3)
print(x %= 3)
print(x &= 3)
print(x = 3)
print(x ^= 3)
print(x <<= 2)
print(x >>= 2)
Output:
5
9
3
18
2
0
2
7
5
8
1
4. Swift Comparison Operator
These operators help to compare two values and return Boolean values as output.
Operator  Symbol  Explanation  Format 
Equal to  ==  Returns true if both operands are equal else return false  x == y 
Not Equal to  !=  Returns true if both operands are not equal; else return false  x != y 
Greater than  >  Returns true if the left operand is greater than right; else return false  x > y 
Less than  <  Returns true if the left operand is smaller than right; else return false  x < y 
Greater than or equal to  >=  Returns true if the left operand is greater than or equal to the right; else return false  x >= y 
Less than or equal to  <=  Returns true if the left operand is smaller than or equal to the right; else return false  x <= y 
Example:
print(5 == 2)
print(5 != 2)
print(5 > 2)
print(5 < 2)
print (5 >= 5)
print (5 <= 2)
Output:
false
true
true
true
true
false
5. Swift Bitwise Operator
Operator  Symbol  Explanation  Format 
Binary AND  &  Check the operands bitwise and return 1 if both bits are 1; else return 0  x & y 
Binary OR    Check the operands bitwise and return 0 if both bits are 0; else return 1  x  y 
Binary XOR  ^  Check the operands bitwise and return 0 if both bits are the same; else return 1  x ^ y 
Binary NOT  ~  Returns ones complement, i.e. changes 1 to 0 and vice versa 
~x

Binary Left Shift  <<  Bits of the left operand is shifted left side by the number of bits mentioned by the right operand  x << 4 
Binary Right Shift  >>  Bits of the left operand is shifted right side by the number of bits mentioned by the right operand  x >> 4 
Example:
var a = 8
var b = 7
print(a & b)
print(a  b)
print(a ^ b)
print(~ b)
print(a << 2)
print(a >> 2)
Output:
0
15
15
8
0
2
6. Swift Range Operators
These operators are used as shortcuts to express the range of values.
Operator  Symbol  Explanation  Format 
Closed Range  (a…b)  It defines a range from a to b, both included  1…5 
Half – Open Range  (a..<b)  It defines the range from a to b; an included while b excluded  1..<5 
Onesided Range 
a… ..a 
It defines the range from a to end of elements or from start to a 
1… …2 
Example:
for i in 1...4 {
print(i)}
for j in 1. . <4 {
print(j) }
let range = ..<4
print(range.contains(2))
Output:
1
2
3
4
1
2
3
true
7. Swift Miscellaneous Operators
Operator  Symbol  Explanation  Format 
Unary Plus  +  This toggles the sign of the numeric value to plus  +5 
Unary Minus  –  This toggles the sign of the numeric value to minus.  6 
Ternary condition  ? :  Used to check a condition and then give output accordingly  Condition? a: b 
Example:
var a = 3
print(+a)
print(a)
let b = (6==6) ? “True”: “false”
print(b)
Output:
3
3
True
Recommended Articles
This has been a guide to Swift Operator. Here we have discussed the various type of Swift operators with examples. You can also go through our other suggested articles to learn more –
30 Online Courses  31 Handson Projects  157+ Hours  Verifiable Certificate of Completion
4.5
View Course
Related Courses