Swift Operators
Operators is an special symbols, which are used to compare,assignment, and perform logical and mathematical operation. That is a simple way to describe the role of an operator. But that is a basic level, operator is much powerful in programming. That are used basic and special purpose.
Generally operator are involved of many place in our code, Such as when, define a similar type of multiple variable then there are use comma operator to separating the variable. Dot operator are used to method of class and there are in similar way many operator are used to perform some specific task.
In this post are given examples which are commonly used to mathematical and logical operation.
Arithmetic Operator
Arithmetic Operator are work in two operands, that is also know as binary operators. We can thinking about this type of operators. This operator are works two operands such as left and right. And operator are exist in between of them. operator is decide to which operation are performed to those operand. In given table mention of all arithmetic operators.
Operator  Name 

+  Addition(+) 
  Subtract () 
*  multiplication (*) 
/  Divide 
%  remainder operator 
Basically arithmetic operator are used to perform 6 type of operation. compiler are know about what is the role of symbol that are applied to operands (variable value or constant literals).
let a = 10 , b = 3
print(" a : \(a) , b : \(b)")
print(" ( \(a) + \(b) ) = \(a+b) ")
print(" ( \(a)  \(b) ) = \(ab) ")
print(" ( \(a) * \(b) ) = \(a*b) ")
print(" ( \(a) / \(b) ) = \(a/b) ") //divide two integer
print(" ( \(a) % \(b) ) = \(a%b) ")
Output
a : 10 , b : 3
( 10 + 3 ) = 13
( 10  3 ) = 7
( 10 * 3 ) = 30
( 10 / 3 ) = 3
( 10 % 3 ) = 1
In Swift Programming, (+) addition operator are used to adding the numeric value. () operator are used to Subtract two values, (*) operator are used to multiplication of two values. and similarly (/) operator are divide the two values and (%) are used to get remainder of two values. Modulus % operator are work only integer values.
Logical Operator
Logical operator are work in boolean values. && operator are used to check expression condition when both left and right the expression are produced boolean true value. Then that are produce an true boolean result. Otherwise produce false. In given list are mentioned logical operators.
Operator  Name 

&&  Logical AND operator. 
  Logical OR Operator 
!  Logical NOT Operator 
Example of logical AND operator.
var result = true && true
print(result) //true
result = false && false
print(result) //false
result = true && false
print(result) //false
result = false && true
print(result) //false/code>
Output
true
false
false
false
Logical Or () is an binary operator, That is work on boolean values. This operator are produce true value when any one expression are produced true result (left and right operand result).
var result = true  true
print(result) //true
result = false  false
print(result) //false
result = true  false
print(result) //true
result = false  true
print(result) //trues
Output
true
false
true
true
Logical Not (!) is an unary operator that are work on boolean value. when expression is produce true value that is modified this result to false. and when expression is produce false boolean value then that is change this result to true boolean value.
let trueStatus = true
let falseStatus = !trueStatus //use logical not
print("trueStatus : \(trueStatus)")
print("falseStatus : \(falseStatus)")
Output
trueStatus : true
falseStatus : false
We can also use this operator to check logical condition result. The main role of Logical Not operator is inverts the boolean result. And this is prefix operator.
if(!(5 > 15)){
// (5>15) = false => !false = true
print("Five is not greater than 15")
}
Output
Five is not greater than 15
Bitwise Operators
Bitwise operator is based on binary value and that are work on two operands. Normally all operators are produce boolean values but that is work on a binary bit level modification. In given table mentioned all Bitwise operator.
Operator  Name 

&  Binary AND operator. 
  Binary OR Operator 
^  Binary XOR Operator 
~  Binary Ones Complement Operator 
<<  Binary Left Shift Operator 
>>  Binary Right Shift Operator 
Binary AND operator
//Example Binary And Operator
/*
* Binary of 5 = 0 1 0 1
* Binary of 4 = 0 1 0 0
* &
*
* Result 4 = 0 1 0 0
*/
var a = 5 & 4
print(a) //4
Output
4
Binary Or operator
//Example Binary OR Operator
/*
* Binary of 3 = 0 0 1 1
* Binary of 4 = 0 1 0 0
* 
*
* Result 7 = 0 1 1 1
*/
var a = 3  4
print(a) //7
Output
7
Binary XOR operator
//Example Binary XOR Operator
/*
* Binary of 5 = 0 1 0 1
* Binary of 4 = 0 1 0 0
^
*
* Result 1 = 0 0 0 1
When 0 1 = 1
When 0 0 = 0
When 1 1 = 0
*/
var a = 5 ^ 4
print(a) //1
Output
1
Binary Ones Complement Operator
var data1: Int = 15
var data2: Int = 6
/*Bitwise 1s Complement Operator
Example 1:
15 = 1 1 1 1 (binary of 15)
add 1 (binary of 1)
+

Result= 1 1 1 1 (binary of 16)

output=  Result
output=  16
Example 2: first remove that ( sign)
6 = 0 1 1 0 (binary of 6)
minus 1 (binary of 1)


Result= 0 1 0 1 (binary of 5)
Result= 5
//add remove sign
Result =5

//multiply by ( sign)
output=  Result
output=  (5)
output= 5
*/
print(~data1)
print(~data2)
Output
16
5
Binary Left Shift Operator
var data1:Int=15
var data2:Int=2
/*Bitwise <<
Example 1:
15 = 0 0 1 1 1 1 (binary of 15)
left shift 2 = 1 1 1 1 0 0

60 = 1 1 1 1 0 0

Or
Formula = data * (2^ left shift)
example 1 = 15 *(2^2) 2 bit shift
= 15*4
= 60
example 2 = 2 *(2^5)
= 2 *(32)
= 64
*/
print(15 << 2)
print(2 << 5)
Output
60
64
Binary Right Shift Operator
var data1:Int=15
var data2:Int=2
/*Bitwise >>
Example 1:
15 = 1 1 1 1 (binary of 15)
right shift 2 = 0 0 1 1

3 = 0 0 1 1

*/
print(15>>2) //3
Output
3
Relational Operators
Relational operator are work on two operands, and normally that are used to compare operands value. This operator are produce boolean result. And which are normally used in conditional statement to check expression.
Operator  Name 

>  greater than 
<  less than 
==  equal to equal to 
>=  greater than equal to 
<=  less than equal to 
!=  Not equal to 
Assignment Operators
Assignment operator are used to assign a value of right hand side to left hand variables. In given table are view short form of assignment operator. which are perform some operation of two operand and result are store in the left side variable.
Operator  Meaning 

=  equal 
*=  Multiply and assign 
+=  Add and assign 
/=  Divide and assign 
=  Subtract and assign 
%=  calculate remainder and assign 
Basically normal (=) assignment operator are used to set value of variable. This are assign value according to data type of variable. But short form we are combine this operator and arithmetic operator and perform two type of operation. First are performed arithmetic operation and resultant are store by assignment operator.
Ternary Conditional Operator
This operators is similar to C Ternary operator. There is divided into three section, first one are expression. If result of first expression result is true then work on second section expression. And if first expression is false then it will executing on third section expression.
//section 1 section 2 section 3
expression/condition ? Pass : Fail
For example
let errorStatus=false
//expression
var result=(errorStatus==true) ? "Sorry Error is Found" : "Awesome Error Free code"
print(result)
Output
Awesome Error Free code
This is actually shortcut form of ifelse statement. And normally that are used to assign value of variables.
Range Operators
There are two type of range operator are available in swift programming. halfclosed range operator and closed range operator.
//halfclosed range operator
lowerLimit ..< upperLimit
//closed range operator
lowerLimit...upperLimit
Two dot and left angle are used to halfclosed range operator. Here lower limit and upper limit is an integer values. Element of this operator are start to lower limit, and end with upper limit 1.
let lowerLimit : Int = 0
let upperLimit : Int = 7
for element in lowerLimit..<upperLimit {
print("\(element)")
}
Output
0
1
2
3
4
5
6
Closed range operator are start with the lower limit, and end with the upper limit.
let lowerLimit : Int = 0
let upperLimit : Int = 7
for element in lowerLimit...upperLimit {
print("\(element)")
}
Output
0
1
2
3
4
5
6
7
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment