# Swift - Operators

Operators are used to perform operation on a single operand or two operands. Operators in Swift can be categorized as follows:

- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
- Range operators
- Miscellaneous operators

## Swift Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands.

Operator | Name | Description |
---|---|---|

+ | Addition | Add two values |

- | Subtraction | Subtract one value from another |

* | Multiplication | Multiply two values |

/ | Division | Divide one value by another |

% | Modulo | Returns remainder of division operation |

Example

## Swift Assignment operators

Assignment operators are used to assign values of right hand side expression to left hand side operand.

Operator | Expression | Equivalent to | Description |
---|---|---|---|

= | a = 5 | a = 5 | Example |

+= | a += b | a = a + b | |

-= | a -= b | a = a - b | |

*= | a *= b | a = a * b | |

/= | a /= b | a = a / b | |

%= | a %= b | a = a % b | |

&= | a &= b | a = a & b | More Info |

|= | a |= b | a = a | b | More Info |

^= | a ^= b | a = a ^ b | More Info |

>>= | a >>= b | a = a >> b | More Info |

<<= | a <<= b | a = a << b | More Info |

## Swift Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.

Operator | Description |
---|---|

== | Equal |

!= | Not equal |

> | Greater than |

< | Less than |

>= | Greater than or equal to |

<= | Less than or equal to |

Example

## Swift Logical operators

Logical operators are used to combine two or more conditions.

Operator | Name | Description |
---|---|---|

&& | Logical AND | Returns true when all conditions are true |

|| | Logical OR | Returns true when any of the conditions is true |

! | Logical NOT | Returns true when given conditions is not true |

More Info

## Swift Bitwise operators

Bitwise operators are used to perform bitwise operations on two operands.

Operator | Name | Description | More Info |
---|---|---|---|

& | AND | Returns 1 if both bits at the same position in both operands are 1, else returns 0. | More Info |

| | OR | Returns 1 if one of two bits at the same position in both operands is 1, else returns 0. | More Info |

^ | XOR | Returns 1 if only one of two bits at the same position in both operands is 1, else returns 0. | More Info |

~ | NOT | Reverse all the bits. | More Info |

>> | Right shift | The left operand is moved right by the number of bits present in the right operand. | More Info |

<< | Left shift | The left operand value is moved left by the number of bits present in the right operand. | More Info |

## Swift Range operators

The table below describes other operators supported by Swift:

Operator | Description | Example |
---|---|---|

Closed Range Operator | (a...b)Defines a range that runs from a to b, and includes the values a and b. The value of a must not be greater than b. | Example |

Half-Open Range Operator | (a..<b)Defines a range that runs from a to b, but does not include b. The value of a must not be greater than b. | |

One-Sided Ranges | (a...)Defines a range that runs from a to end of elements.(...a) Defines a range starting from start to a. |

## Swift Miscellaneous operators

The table below describes other operators supported by Swift:

Operator | Description |
---|---|

ternary operator (?:) | Returns one of the two values based on value of boolean expression. |

Nil-Coalescing Operator (??) | Returns right-hand side operand when left-hand side operand is nil, otherwise returns left-hand side operand. |

Unary plus | Returns the value it operates on, without any change. For example: if A = 10 then +A gives 10. |

Unary minus | Returns the value it operates on with sign changed. For example: if A = -10 then -A gives 10. |

## Swift Operators Precedence

Operator precedence (**order of operations**) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 5^{2} = 28 and 3 × 5^{2} = 75.

Swift has predefined - **prefix**, **postfix** and **infix** operators. The **prefix** operators are as follows:

- ! Logical NOT
- .! Pointwise logical NOT
- ~ Bitwise NOT
- + Unary plus
- - Unary minus
- ..< Half-open range
- ... Closed range

The **postfix** operators are as follows:

- ... Range

The following table lists the precedence and associativity of Swift **infix** operators. Operators are listed top to bottom, in **descending precedence**. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.

Precedence | Operator | Description | Associativity |
---|---|---|---|

16 | << >> | Bitwise left shift and right shift | non-associative |

&<< &>> | Bitwise left shift and right shift, ignoring overflow | ||

15 | * / % | Multiplication, Division, Remainder | Left to Right |

& | Bitwise AND | ||

&* | Multiply with overflow | ||

14 | + - | Addition, Subtraction | |

| ^ | Bitwise OR, Bitwise XOR | ||

&+ &- | Add with overflow, Subtract with overflow | ||

13 | ..< ... | Half-open range, Closed range | non-associative |

12 | is | Type check | Left to Right |

as as? as! | Type cast | ||

11 | ?? | Nil coalescing operator | Right to Left |

10 | < <= > >= | Less than, Less than or equal, Greater than, and Greater than or equal | non-associative |

9 | == != | Equality and Inequality | |

8 | === !== | Identical and Not identical | |

7 | ~= | Pattern match | |

6 | .< .<= .> .>= | Pointwise less than, Pointwise less than or equal, Pointwise greater than, and Pointwise greater than or equal | |

5 | .== .!= | Pointwise equality and Pointwise inequality | |

4 | && .& | Logical AND, Pointwise bitwise AND | Left to Right |

3 | || .| .^ | Logical OR, Pointwise bitwise OR, Pointwise bitwise XOR | |

2 | a?b:c | Conditional (ternary) operator | Right to Left |

1 | = | Direct assignment | |

+= -= *= /= %= | Compound assignment by sum, difference, product, quotient and remainder | ||

<<= >>= | Compound assignment by Bitwise left shift and right shift | ||

&= ^= |= | Compound assignment by Bitwise AND, XOR and OR | ||

&+= &-= &*= | Add, Subtract and Multiply with overflow and assign | ||

&<<= &>>= | Assign by Left bit shift and Right bit shift ignoring overflow | ||

.&= .|= .^= | Assign by Pointwise bitwise AND, Pointwise bitwise OR, Pointwise bitwise XOR |