Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kotlin Programming Cookbook

You're reading from   Kotlin Programming Cookbook Explore more than 100 recipes that show how to build robust mobile and web applications with Kotlin, Spring Boot, and Android

Arrow left icon
Product type Paperback
Published in Jan 2018
Publisher
ISBN-13 9781788472142
Length 434 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Aanand Shekhar Roy Aanand Shekhar Roy
Author Profile Icon Aanand Shekhar Roy
Aanand Shekhar Roy
Rashi Karanpuria Rashi Karanpuria
Author Profile Icon Rashi Karanpuria
Rashi Karanpuria
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Installation and Working with Environment FREE CHAPTER 2. Control Flow 3. Classes and Objects 4. Functions 5. Object-Oriented Programming 6. Collections Framework 7. Handling File Operations in Kotlin 8. Anko Commons and Extension Function 9. Anko Layouts 10. Databases and Dependency Injection 11. Networking and Concurrency 12. Lambdas and Delegates 13. Testing 14. Web Services with Kotlin 15. Other Books You May Enjoy

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.

You have been reading a chapter from
Kotlin Programming Cookbook
Published in: Jan 2018
Publisher:
ISBN-13: 9781788472142
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image