Swift - Assignment Operators



Assignment Operators are the special operators. They are used to assign or update values to a variable or constant. In the assignment operators, the right-hand side of the assignment operator is the value and the left-hand side of the assignment operator should be the variable to which the value will be assigned.

The data type of both sides should be the same, if they are different we will get an error. The associativity of assignment operators is from right to left. Swift supports two types of assignment operators −

  • Simple Assignment Operator − It is the most commonly used operator in Swift. It is used to assign value to a variable or constant.

  • Compound Assignment Operators − They are the combination of assignment operator (=) with other operators like +, *, /, etc.

The following table shows all the assignment operators supported by Swift −

Operators Name Example
(=) Assignment var x = 10
+= Add and Assignment A += B is equivalent to A = A + B
-= Subtract and Assignment A -= B is equivalent to A = A - B
*= Multiply and Assignment A *= B is equivalent to A = A * B
/= Divide and Assignment A /= B is equivalent to A = A / B
%= Modulo and Assignment A %= B is equivalent to A = A % B
&= Bitwise AND and Assignment A &= B is equivalent to A = A & B
|= Bitwise Inclusive OR and Assignment A += B is equivalent to A = A | B
^= Bitwise Exclusive OR and Assignment A ^= B is equivalent to A = A ^B
<<= Left Shift and Assignment A <<= B is equivalent to A = A << B
>>= Right Shift and Assignment A >>= B is equivalent to A = A >> B

Assignment Operator in Swift

An assignment operator "=" is the most straightforward and commonly used operator in Swift. It is used to assign value to a constant or variable. The left-hand side of the assignment operator contains the variable name and the right-hand side contains the value.

While using the assignment operator always remember the data type of both the operands should be the same.

Syntax

Following is the syntax of the assignment operator −

var number = 10 

Example

Swift program to assign a string to a variable.

import Foundation
// Defining a variable of string type
let mystring : String

// Assigning a value to the variable using the assignment operator
mystring = "Tutorialspoint"

// displaying result
print("String = ", mystring)

Output

String = Tutorialspoint

Add and Assignment Operator in Swift

An Add and Assignment Operator "+=" is used to perform addition between the left variable and right variable and then assign the result to the left variable. Suppose we have two variables A = 10 and B = 20. A += B => A = 10 + 20 => A = 30.

Syntax

Following is the syntax of the add and assignment operator −

X += Y

Example

Swift program to find the sum of two variables using add and assignment operator "+=".

import Foundation
var num1 = 10
var num2 = 80

// Calculating sum
num1 += num2
print("Sum = ", num1)

Output

Sum =  90

Subtract and Assignment Operator in Swift

A Subtract and Assignment Operator "-=" is used to perform a subtraction between the left variable and the right variable and then assign the result to the left variable. Suppose we have two variables A = 10 and B = 20. A -= B => A = 10 - 20 => A = -10.

Syntax

Following is the syntax of the subtract and assignment operator −

X -= Y

Example

Swift program to subtract two variables using subtract and assignment operator "-=".

import Foundation
var num1 = 34
var num2 = 21

// Subtracting num1 from num2
num1 -= num2

print("Result = ", num1)

Output

Result =  13

Multiply and Assignment Operator in Swift

A Multiply and Assignment Operator "*=" is used to perform a multiplication between the left operand and the right operand and then assign the result to the left operand. Suppose we have two variables A = 10 and B = 20. A *= B => A = 10 * 20 => A = 200.

Syntax

Following is the syntax of the multiply and assignment operator −

X *= Y

Example

Swift program to find the product of two variables using multiply and assignment operator "*=".

import Foundation
var num1 = 12
var num2 = 2

// Product of two numbers
num1 *= num2
print("Result = ", num1)

Output

Result = 24

Divide and Assignment Operator in Swift

A Divide and Assignment Operator "/=" is used to divide the left operand by the right operand and then assign the result to the left operand. Suppose we have two variables A = 20 and B = 5. A /= B => A = 20 / 5 => A = 4.

Syntax

Following is the syntax of the divide and assignment operator −

X /= Y

Example

Swift program to find the divide two variables using divide and assignment operator "/=".

import Foundation
var num1 = 12
var num2 = 2

// Dividing two numbers
num1 /= num2
print("Result = ", num1)

Output

Result = 6

Modulo and Assignment Operator in Swift

A Modulo and Assignment Operator "%=" is used to calculate the modulus or remainder of two operands and assign the result to the left operand. For example, we have two variables A = 20 and B = 5. A %= B => A = 20 & 5 => A = 0.

Syntax

Following is the syntax of the modulo and assignment operator −

X %= Y

Example

Swift program to find the modulus of two variables using modulo and assignment operator "%=".

import Foundation
var num1 = 13
var num2 = 2

// Modulus two numbers
num1 %= num2
print("Result = ", num1)

Output

Result =  1

Bitwise AND and Assignment Operator in Swift

A Bitwise AND and Assignment Operator "%=" is used to perform AND operation between two variables or operands and assign the final result to the left variable. For example, we have two variables A = 9 and B = 10. A &= B => A = 9 & 10 => A = 8.

Syntax

Following is the syntax of the bitwise AND and assignment operator −

X &= Y

Example

Swift program to bitwise AND in between two variables using bitwise AND and assignment operator "&=".

import Foundation
var num1: UInt8 = 0b11001100
let num2: UInt8 = 0b00110011

// Performing a bitwise AND operation
num1 &= num2 
print("Result=", num1)

Output

Result= 0

Bitwise Inclusive OR and Assignment Operator in Swift

A Bitwise Inclusive OR and Assignment Operator "^=" is used to perform OR operation between two variables or operands and assign the final result to the left-hand side variable.

For example, we have two variables A = 9 and B = 10. A ^= B => A = 9 ^ 10 => A = 3.

Syntax

Following is the syntax of the bitwise inclusive OR and assignment operator −

X ^= Y

Example

Swift program to bitwise OR in between two variables using bitwise OR and assignment operator "^=".

import Foundation

var num1: UInt8 = 0b11001100
let num2: UInt8 = 0b11101000

// Performing a bitwise inclusive OR operation
num1 ^= num2 

print("Result=", num1)

Output

Result= 36

Bitwise Exclusive OR and Assignment Operator in Swift

A Bitwise Exclusive OR and Assignment Operator "|=" is used to perform exclusive OR operation between two variables or operands and assign the final result to the left-hand side variable.

For example, we have two variables A = 10 and B = 10. A |= B => A = 9 | 10 => A = 10.

Syntax

Following is the syntax of the bitwise exclusive OR and assignment operator –

X |= Y

Example

Swift program to bitwise OR in between two variables using bitwise OR and assignment operator "|=".

import Foundation
var num1: UInt8 = 0b11001100
let num2: UInt8 = 0b11101000

// Performing a bitwise exclusive OR operation
num1 |= num2 
print("Result=", num1)

Output

Result= 236

Left Shift and Assignment Operator in Swift

A Left Shift and Assignment Operator "<<=" is used to shift the bits of the given operand on the left side according to the given positions and assign the final result to the left-hand side variable.

Syntax

Following is the syntax of the left shift and assignment operator −

X <<= Y

Example

Swift program to shift the bits of the given value on the left side using the left shift and assignment operator "<<=".

import Foundation
var number: UInt8 = 0b00001100 

// Shift the bits to the left by 3 positions
number <<= 3

// Print the result
print("Result: \(String(number, radix: 2))") 

Output

Result: 1100000

Right Shift and Assignment Operator in Swift

A Right Shift and Assignment Operator ">>=" is used to shift the bits of the given operand on the right side according to the given positions and assign the final result to the left-hand side variable.

Syntax

Following is the syntax of the right shift and assignment operator −

X >>= Y

Example

Swift program to shift the bits of the given value on the right side by 4 positions using the right shift and assignment operator ">>=".

import Foundation
var number: UInt8 = 0b11001000
// Shift the bits to the right by 4 positions
number >>= 4

// Print the result
print("Result: \(String(number, radix: 2))") 

Output

Result: 1100
swift_operators.htm
Advertisements