Book Image

Kotlin Programming Cookbook

By : Aanand Shekhar Roy, Rashi Karanpuria
Book Image

Kotlin Programming Cookbook

By: Aanand Shekhar Roy, Rashi Karanpuria

Overview of this book

The Android team has announced first-class support for Kotlin 1.1. This acts as an added boost to the language and more and more developers are now looking at Kotlin for their application development. This recipe-based book will be your guide to learning the Kotlin programming language. The recipes in this book build from simple language concepts to more complex applications of the language. After the fundamentals of the language, you will learn how to apply the object-oriented programming features of Kotlin 1.1. Programming with Lambdas will show you how to use the functional power of Kotlin. This book has recipes that will get you started with Android programming with Kotlin 1.1, providing quick solutions to common problems encountered during Android app development. You will also be taken through recipes that will teach you microservice and concurrent programming with Kotlin. Going forward, you will learn to test and secure your applications with Kotlin. Finally, this book supplies recipes that will help you migrate your Java code to Kotlin and will help ensure that it's interoperable with Java.
Table of Contents (21 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Doing bit manipulations in Kotlin


Kotlin provides several functions (in infix form) to perform bitwise and bit shift operations. In this section, we will learn to perform bit-level operation in Kotlin with the help of examples.

Bitwise and bit shift operators are used on only two integral types—Int and Long—to perform bit-level operations.

Getting ready

Here's the complete list of bitwise operations (available for Int and Long only):

  • shr(bits): signed shift right (Java's >>)
  • ushr(bits): unsigned shift right (Java's >>>)
  • and(bits): bitwise and
  • or(bits): bitwise or
  • xor(bits): bitwise xor
  • inv(): bitwise inversion

How to do it...

Let's check out a few examples to understand the bitwise operations.

Or

The or function compares the corresponding bits of two values. If either of the two bits is 1, it gives 1, and it gives 0 if not.

Consider this example:

fun main(args: Array<String>) {
  val a=2
  val b=3
  print(a or b)
}

The following is the output:

 3

Here's the explanation of the preceding example:

2 = 10 (Binary format)

3 = 11 (Binary format)

 Bitwise OR of 2 and 3 that is

in binary 

 10 OR 11

 11 = 3 (Decimal format)

and

The and function compares the corresponding bits of two values. If either of the two bits is 0, it gives 0, if not and both bits are 1, it gives 1.

Consider this example:

fun main(args: Array<String>) {
  val a=2
  val b=3
  print(a and b)
}

 

This is the output:

 2

Let's look at the explanation:

2 = 10 (Binary format)

3 = 11 (Binary format)

Bitwise AND of 2 and 3

              in binary

10 AND 11

10 = 2 (Decimal format)

xor

The xor function compares the corresponding bits of two values. If the corresponding bits are the same, it gives 0, and if they are different, it gives 1.

Look at this example:

fun main(args: Array<String>) {
  val a=2
  val b=3
  print(a xor b)
}

Given is the output:

 1

Here's the explanation:

2 = 10 (Binary format)

3 = 11 (Binary format)

Bitwise XOR of 2 and 3

                    in binary

10 XOR 11

01 = 1 (Decimal format)

inv

The inv function simply inverts the bit patterns. If the bit is 1, it makes it 0 and vice versa.

Here's an example:

fun main(args: Array<String>) {
    val a=2
   print(a.inv())}

This is the output:

 -3

The following is the explanation:

2 = 10 (Binary format)

Bitwise complement of 2 = 01, but the compiler shows 2’s complement of that number, which is the negative notation of the binary number.

2’s complement of an integer n is equal to -(n+1).

 

shl

The shl function shifts the bit pattern to the left by the specified number of bits.

Consider this example:

fun main(args: Array<String>) {
       println( 5 shl 0)
       println( 5 shl 1)
       println( 5 shl 2)
}

This is the output:

5
10
20

Here's the explanation:

5 = 101 (Binary format)

101 Shift left by 0 bits = 101

101 Shift left by 1 bits = 1010 (10 in Decimal)

101 Shift left by 2 bits = 10100 (20 in Decimal)

 

shr

The shr function shifts the bit pattern to the right by the specified number of bits.

Take this example into consideration:

fun main(args: Array<String>) {
       println( 5 shr 0)
       println( 5 shr 1)
       println( 5 shr 2)
}

Given here is the output:

5
2
1

The following is the explanation:

5 = 101 (Binary format)

101 Shift right by 0 bits = 101

101 Shift right by 1 bits = 010 (2 in Decimal)

101 Shift right by 2 bits = 001 (1 in Decimal)

ushr

The ushr function shifts the bit pattern to the right by the specified number of bits, filling the leftmost with 0s.

Here's an example:

fun main(args: Array<String>) {
       println( 5 ushr 0)
       println( 5 ushr 1)
       println( 5 ushr 2)
}

This will output the following:

5
2
1

This is its explanation:

5 = 101 (Binary format)

101 Shift right by 0 bits = 101

101 Shift right by 1 bits = 010 (2 in Decimal)

101 Shift right by 2 bits = 001 (1 in Decimal)

How it works...

The bitwise operators in Kotlin aren’t built-in operators like in Java, but they can still be used as an operator. Why? Look at its implementation:

public infix fun shr(bitCount: Int): Int

You can see that the method has the infix notation, which enables it to be called as an infix expression.