## 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.