Swift Operators

In this tutorial, we will learn about different operators in Swift programming language.

Operators in Swift language can be grouped into following categories based on the type of operation they perform.

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Identity Operators
  • Bitwise Operators

In this tutorial, we will go through each of these categories, but, the following tutorials cover these in detail.

Now, let us go through each of the operator categories.

1. Arithmetic Operators

Arithmetic Operators are used to perform basic mathematical arithmetic operators like addition, subtraction, multiplication, etc. The following table lists out all the arithmetic operators in Swift.

Operator SymbolNameExampleDescription
+Additionx + yReturns the sum of values in x and y.
Subtractionx – yReturns the subtraction y from x.
*Multiplicationx * yReturns the product of values in x and y.
/Divisionx / yReturns the quotient in the division of x by y.
%Modulusx % yReturns the remainder in the division of x by y.
Swift Arithmetic Operators

In the following program, we will take values for variables x and y, and perform arithmetic operations on these values using Swift Arithmetic Operators.

main.swift

var x = 5
var y = 2

var result = 0

print("Addition")
result = x + y
print(result)

print("\nSubtraction")
result = x - y
print(result)

print("\nMultiplication")
result = x * y
print(result)

print("\nDivision")
result = x / y
print(result)

print("\nModulo")
result = x % y
print(result)

Output

2. Assignment Operators

Assignment Operators are used to assign or store a specific value in a variable. The following table lists out all the assignment operators in Swift.

Operator SymbolDescriptionExampleEquivalent to
=Assignmentx = y
+=Addition Assignmentx += yx = x + y
-=Subtraction Assignmentx -= yx = x – y
*=Multiplication Assignmentx *= yx = x * y
/=Division Assignmentx /= yx = x / y
%=Modulus Assignmentx %= yx = x % y
**=Exponentiation Assignmentx **= yx = x ** y
//=Floor-division Assignmentx //= yx = x // y
&=AND Assignmentx &= yx = x & y
|=OR Assignmentx |= yx = x | y
^=XOR Assignmentx ^= yx = x ^ y
<<=Zero fill left shift Assignmentx <<= yx = x << y
>>=Signed right shift Assignmentx >>= yx = x >> y
Swift Assignment Operators

In the following program, we will take values for variables x and y, and perform assignment operations on these values using Swift Assignment Operators.

main.py

x, y = 5, 2
x += y
print(x) # 7

x, y = 5, 2
x -= y
print(x) # 3

x, y = 5, 2
x *= y
print(x) # 10

x, y = 5, 2
x /= y
print(x) # 2.5

x, y = 5, 2
x %= y
print(x) # 1

x, y = 5, 2
x **= y
print(x) # 25

x, y = 5, 2
x //= y
print(x) # 2

x, y = 5, 2
x &= y
print(x) # 0

x, y = 5, 2
x |= y
print(x) # 7

x, y = 5, 2
x ^= y
print(x) # 7

x, y = 5, 2
x <<= y
print(x) # 20

x, y = 5, 2
x >>= y
print(x) # 1

3. Bitwise Operators

Bitwise Operators are used to perform bit level operations. The following table lists out all the bitwise operators in Swift.

Operator SymbolDescriptionExample
&ANDx & y
|ORx | y
^XORx ^ y
~NOT~x
<<Zero fill left shiftx << y
>>Signed right shiftx >> y
Swift Bitwise Operators

main.py

# AND
x, y = 5, 2
print(x & y) # 0

# OR
x, y = 5, 2
print(x | y) # 7

# XOR
x, y = 5, 2
print(x ^ y) # 7

# NOT
x, y = 5, 2
print(~x) # -6

# Zero fill left shift
x, y = 5, 2
print(x << y) # 20

#Signed right shift
x, y = 5, 2
print(x >> y) # 1

4. Comparison Operators

Comparison Operators are used to compare two operands. The following table lists out all the Comparison operators in Swift.

Operator SymbolDescriptionExample
==Equal tox == y
!=Not Equal tox != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y
Swift Comparison Operators

main.py

# Equal to
x, y = 5, 2
print(x == y) # False

# Not equal	to
x, y = 5, 2
print(x != y) # True

# Greater than	
x, y = 5, 2
print(x > y) # True

# Less than	
x, y = 5, 2
print(x < y) # False

# Greater than or equal to	
x, y = 5, 2
print(x >= y) # True

# Less than or equal to
x, y = 5, 2
print(x <= y) # False

5. Logical Operators

Logical Operators are used to combine simple conditions and form compound conditions. The following table lists out all the Logical operators in Swift.

Operator SymbolDescriptionExample
andReturns True if both operands are True.x and y
orReturns True if any of the operands is True.x or y
notReturns the complement of given boolean operand.not x
Swift Logical Operators

main.py

# Logical AND
x, y = True, False
print(x and y) # False

# Logical OR
x, y = True, False
print(x or y) # True

# Logical NOT
x = True
print(not x) # False

Conclusion

In this Swift Tutorial, we learned about different kinds of Operators in Swift language: Arithmetic, Assignment, Comparison, Logical, and Bitwise, with examples.