EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Swift Tutorial Swift Operators
 

Swift Operators

Priya Pedamkar
Article byPriya Pedamkar

Updated June 29, 2023

Swift Operators

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

They are classified into three types:

  1. Unary operators: These operators operate on a single operand only. For example, increment operator.
  2. Binary operators: These operators operate on two operands. For example, the addition operator.
  3. 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
One-sided 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

We hope that this EDUCBA information on “swift operators” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Swift vs Objective C
  2. Swift and Go
  3. C Operators
  4. MySQL Operators

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW