2
2
.
.
1
1
.
.
3
3
O
O
p
p
e
e
r
r
a
a
t
t
o
o
r
r
s
s
I
I
n
n
f
f
o
o
Operators are used to compare or combine variables and their values.
A
A
s
s
s
s
i
i
g
g
n
n
m
m
e
e
n
n
t
t
Assignment operators assign value to variable.
Shortcut assignment operator += performs mathematical operation before making the assignment.
Assignment
//TEST VARIABLES.
var x = 15
var str = "Hello"
//ASSIGNMENT OPERATORS.
x = 15 // Assignment
x += 10 // Addition-Assignment: x = x + 10
C
C
o
o
m
m
p
p
a
a
r
r
i
i
s
s
o
o
n
n
Comparison operators are used to compare values of two variables.
This way they provide a method to direct program flow depending on the outcome.
SWIFT comparison operators should be used only for comparing numerical values.
Strings should be compared using predefined SWIFT functions designed for that purpose.
Comparison
//TEST VARIABLES.
var left = 65
var right = 70
//COMPARISON OPERATORS.
if (left == right) { print("Left equals right.") } // Equal
if (left != right) { print("Left is different from right.") } // Not equal
if (left < right) { print("Left is smaller then right.") } // Less than
if (left <= right) { print("Left is smaller or equals right.") } // Less than or equal
if (left > right) { print("Left is greater then right.") } // Greater than
if (left >= right) { print("Left is greater or equals right.") } // Greater than or equal
A
A
r
r
i
i
t
t
h
h
m
m
e
e
t
t
i
i
c
c
Arithmetic operators are used to perform mathematical operations.
Mathematical operations, not supported by arithmetic operators, can be performed using SWIFT mathematical functions.
Arithmetic
//TEST VARIABLES.
var x = 10
var y = 20
//ARITHMETIC OPERATORS.
var negate = -y // Negation -20 = -20
var subtract = x - y // Subtraction 10-20 = -10
var multiply = x * y // Multiplication 10*20 = 200
var divide = x / y // Division 10/20 = 0.5
var modulo = x % y // Modulus 10%20 = 0*20+10 = 10
y += 5 // Increment by 5
y -= 5 // Decrement by 5
L
L
o
o
g
g
i
i
c
c
a
a
l
l
Logical operators are used to combine boolean values that can be only TRUE or FALSE.
This way they provide a method to direct program flow depending on the outcome.
Logical
//TEST VARIABLES.
var left = 65
var right = 70
//LOGICAL OPERATORS.
if ( !(left == 80)) { print("left is NOT equal to 80 \n") } // NOT True if b is not true
if ( left > 50 && right == 70 ) { print("left >50 AND right==70 \n") } // AND True if both are true
if ( left != 50 || right > 90 ) { print("left!=50 OR right> 90 \n") } // OR True if either is true
B
B
i
i
t
t
w
w
i
i
s
s
e
e
Bitwise operators combine bits within one or two integers.
Left shift <<n is Arithmetic Shift equivalent of multiplying by 2
n
which preserves operand sign.
Right shift >>n is Arithmetic Shift equivalent of dividing by 2
n
which preserves operand sign.
AND, OR, XOR and NOT do not change the value of operands.
Bitwise
//TEST VARIABLES.
var left = 27 //11011
var right = 18 //10010
//BITWISE OPERATORS.
var and = left & right //10010. 1 if both bits are 1.
var or = left | right //11011. 1 if either bit is 1.
var xor = left ^ right //01001. 1 if bits are different.
var invert = ~left //11111111111111111111111111100100. Invert bits.
var shiftLeft = left << 3 //11011000. Shift bits to left by 3 positions. Fill with 0.
var shiftRight = left >> 2 //110. Shift bits to right by 2 positions. Fill with sign bit.
//DISPLAY RESULTS.
print(and)