# Operators in Kotlin

In this tutorial, we will learn about Operators in Kotlin. Operators are special symbols in Kotlin and we use operators to perform some special operators on operands. For example, the addition of two variables. In kotlin operators are categorised in various categories.

## Types of Operators:

• Arithmetic operators
• Relational operators
• Assignment operators
• Unary operators
• Bitwise operations
• Logical operators

## Arithmetic Operators:

Arithmetic Operators are used to performing simple mathematical operations like addition, subtraction, multiplication, division etc.

## Example Program:

```fun main(args : Array<String>) {
var num1=20;
var num2=10;
println(num1+num2);
println(num1-num2);
println(num1*num2);
println(num1/num2);
println(num1%num2);
}  ```

## Relational operators:

Relational Operators are used to checking the relation between the two operands. For example to check both operands are equal (num1==num2).

## Example Program:

```fun main(args : Array<String>) {
val num1 = 20
val num2 = 10
if (num1 > num2) {
println("num1 is greater")
} else{
println("num2 is greater")
}
}  ```

## Assignment operators:

Assignment Operators are used to assigning value to a variable. We mainly use = operator as assignment operator but we have several other assignment operators in Kotlin.

## Example Program:

```fun main(args : Array<String>) {

var num1 =20
var num2=10

num1 +=num2
println("num1+=num2 :"+ num1)
num1 -=num2
println("num1-=num2 :"+ num1)
num1 *=num2
println("num1*=num2 :"+ num1)
num1 /=num2
println("num1/=num2 :"+ num1)
num1 %=num2
println("num1%=num2 :"+ num1)

}  ```

## Unary operators:

Unary Operators works on a single operand and there is total of five Unary operators available in Kotlin.

## Example Program:

```fun main(args: Array<String>){
var num1=20
var num2=10
var light = true
println("+num1 :"+ +a)
println("-num2 :"+ -b)
println("++num1 :"+ ++a)
println("--num2 :"+ --b)
println("!light :"+ !flag)
}  ```

## Bitwise operations:

Bitwise operators works at the bit level of operand. But Bitwise operators are not available in Kotlin. There are some predefined functions available in Kotlin which helps us to perform Bitwise operations.

## Example Program:

```fun main(args: Array<String>){
var num1=20
var num2=2

println("Signed shift left: "+num1.shl(num2))
println("Signed shift right: "+num1.shr(num2))
println("Unsigned shift right: "+num1.ushr(num2))
println("Bitwise and: "+num1.and(num2))
println("Bitwise or: "+num1.or(num2))
println("Bitwise xor: "+num1.xor(num2))
println("Bitwise inverse: "+num1.inv())

}  ```

## Logical operators:

Logical operators are used to check Logical relation between Operands. The result of these operators comes in Boolean form true/false. There are three Logical Operators in Kotlin.

## Example Program:

```fun main(args: Array<String>){
var num1=20
var num2=15
var num3=10
var light = true
var result: Boolean
result = (num1>num2) && (num1>num3)
println("Logical AND :"+ result)
result = (num1>num2) || (num1>num3)
println("Logical OR :"+ result)
result = !light
println("Logical NOT :"+ result)
}  ```