DEV Community

Cover image for Operators in Kotlin
Amandeep Singh
Amandeep Singh

Posted on • Updated on

Operators in Kotlin

In this blog we will cover:

  • What are operators
  • Types of operators
  • Arithmetic Operators
  • Increment and Decrement Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators

Operators in Kotlin

What is an Operator?
An operator, in computer programing, is a symbol that usually represents an action or process. These symbols were adapted from mathematics and logic.
Operators are the backbone of any program and they are used for everything from very simple functions like counting to complex algorithms like security encryption.

We have different types of operators
Different types of operators
Now, we will go through each type of operator one by one.
1.Arithmetic Operator: An operator that performs arithmetic operations on groups of variables and numbers is called Arithmetic operator.

fun main(){
    var a = 20 var b = 4 
        println("a + b = " + (a + b))
        println("a - b = " + (a - b))
        println("a * b = " + (a*b))
        println("a / b = " + (a / b))
        println("a % b = " + (a % b))
}
Enter fullscreen mode Exit fullscreen mode

Output:

a + b = 24
a - b = 16
a * b = 80
a / b = 5
a % b = 0

Enter fullscreen mode Exit fullscreen mode

You are familiar with all the operators because you use them in your daily life..but what does "%" this sign does?
This sign is called modulo(%).
OK, but what does it do?
The modulo division operator produces the remainder of an integer division. For example, If 5 and 2 are integers, then the expression:
5 % 2
produces the remainder when 5 is divided by 2, which is 1

2.Increment and Decrement Operators: The increment(++) and decrement operators(--) are important unary operators in Kotlin. Unary operators are those which are applied on a single operand. The increment operator increases the value of the variable by one and the decrement operator decreases the value of the variable by one.

Types of Increment Operators:

  • Prefix Increment Operator: If you use the ++ operator as a prefix like: ++var, the value of var is incremented by 1, then it returns the value.
fun main(){
    val num = 5
    println("num = "+ num++)//First the value of num is printed and then a1 increases by 1.
}
Enter fullscreen mode Exit fullscreen mode

Output:

num = 5
Enter fullscreen mode Exit fullscreen mode
  • Postfix Increment Operator: If you use the ++ operator as a postfix like: var++, the original value of var is returned first, then var is incremented by 1.
fun main(){
    val num = 5
    println("num = "+ ++num) //First the value of a2 increases by 1 and then the new value of num is printed.
}
Enter fullscreen mode Exit fullscreen mode

Output:

num = 6
Enter fullscreen mode Exit fullscreen mode

3.Assignment Operators: Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error. Assignment operator return a Boolean Values.
Different types of assignment operators are shown below:
Different types of assignment operators
Here is the code for your better understanding.

fun main(){
    var c = 30
    var d = 40
    println("c > d = "+(c>d))
    println("c < d = "+(c<d))
    println("c >= d = "+(c>=d))
    println("c <= d = "+(c<=d))
    println("c == d = "+(c==d))
    println("c != d = "+(c!=d))
}

Enter fullscreen mode Exit fullscreen mode

Output:

c > d = false
c < d = true
c >= d = false
c <= d = true
c == d = false
c != d = true
Enter fullscreen mode Exit fullscreen mode

4.Logical Operators: Logical operators are generally used for combining two or more relational statements. They return Boolean values. The logical operators are used primarily in the expression evaluation to make a decision. These operators allow the evaluation and manipulation of specific bits within the integer.

The types of Logical operators with their description are tabulated as follows -
types of Logical operators

fun main(){
    var x = 100
    var y = 25
    var z = 10
    var result = false
    if(x > y && x > z){
    println(x)
}
    if(x < y || x > z){
    println(y)
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:

100
25
Enter fullscreen mode Exit fullscreen mode

5.Bitwise Operators:The bitwise operators are the operators used to perform the operations on the data at the bit-level. When we perform the bitwise operations, then it is also known as bit-level programming. It consists of two digits, either 0 or 1. It is mainly used in numerical computations to make the calculations faster. Bitwise and bit shift operators are used on only two integral types (Int and Long) to perform bit-level operations.

We have different types of bitwise operators in the Kotlin. The following is the list of the bitwise operators:

different types of bitwise operators

  • or: The or function compares corresponding bits of two values. If either of the bits is 1, it gives 1. If not, it gives 0. For example, example of or bitwise Let's implement this in our code!
fun main() {

    val number1 = 12
    val number2 = 25
    val result: Int

    result = number1 or number2   // result = number1.or(number2)
    println(result)
}
Enter fullscreen mode Exit fullscreen mode

Output:

29
Enter fullscreen mode Exit fullscreen mode
  • and: The and function compares corresponding bits of two values. If both bits are 1, it is evaluated to 1. If either of the bits is 0, it is evaluated to 0. For example, example of and bitwise Let's implement this in our code!
fun main() {

    val number1 = 12
    val number2 = 25
    val result: Int

    result = number1 and number2   // result= number1.and(number2)
    println(result)
}
Enter fullscreen mode Exit fullscreen mode

Output:

8
Enter fullscreen mode Exit fullscreen mode
  • xor: The xor function compares corresponding bits of two values. If corresponding bits are different, it gives 1. If corresponding bits are same, it gives 0. For example, example of xor Let's implement this in our code!
fun main() {

    val number1 = 12
    val number2 = 25
    val result: Int

    result = number1 xor number2   // result= number1.xor(number2)
    println(result)
}
Enter fullscreen mode Exit fullscreen mode

Output:

21
Enter fullscreen mode Exit fullscreen mode
  • inv(): The inv() function inverts the bit pattern. It makes every 0 to 1, and every 1 to 0. For example, example of inv()

Let's implement this in our code!

fun main() {

    val number = 35
    val result: Int

    result = number.inv()
    println(result)
}
Enter fullscreen mode Exit fullscreen mode

Output:

-36
Enter fullscreen mode Exit fullscreen mode

Why are we getting output -36 instead of 220?
It's because the compiler is showing 2's complement of that number; negative notation of the binary number.

For any integer n, 2's complement of n will be -(n+1).
The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36. Hence, the output is -36 instead of 220.

  • shl: The shl function shifts bit pattern to the left by certain number of specified bits, and zero bits are shifted into the low-order positions. For example, example of shl

Let's implement this in our code!

fun main() {

    val number = 212

    println(number shl 1)
    println(number shl 0)
    println(number shl 4)
}
Enter fullscreen mode Exit fullscreen mode

Output:

424
212
3392
Enter fullscreen mode Exit fullscreen mode
  • shr: The shr function shifts bit pattern to the right by certain number of specified bits. For example, example of shr

Let's implement this in our code!

fun main() {

    val number = 212

    println(number shr 1)
    println(number shr 0)
    println(number shr 8)
}
Enter fullscreen mode Exit fullscreen mode

Output:

106
212
0
Enter fullscreen mode Exit fullscreen mode
  • ushr: The ushr function shifts zero into the leftmost position. For example, example of ushr

Let's implement this in our code!

fun main() {

    val number1 = 5
    val number2 = -5

    // Signed right shift
    println(number1 shr 1)

    // Unsigned right shift
    println(number1 ushr 1)

    // Signed right shift
    println(number2 shr 1)

    // Unsigned right shift
    println(number2 ushr 1)
}
Enter fullscreen mode Exit fullscreen mode

Output:

2
2
-3
2147483645
Enter fullscreen mode Exit fullscreen mode

Notice, how signed and unsigned right shift function works differently for 2's complement.

The 2's complement of 2147483645 is 3.

These are they types of operators we have in Kotlin. Hope you understood them well as you will be using them on a regular basis as an android developer😊.

You have doubt in any part you can ask it in the discussion section.
Wanna connect? then connect with me on LinkedIn

Thank You

Top comments (0)