Kotlin Bitwise Operators

Introduction

Bitwise operators in Kotlin are used to perform bit-level operations on integer types. These operators are essential for tasks that require direct manipulation of individual bits within an integer. This chapter will cover the different types of bitwise operators available in Kotlin, along with examples and their syntax.

Types of Bitwise Operators

1. and (Bitwise AND)

The bitwise AND operator compares each bit of its first operand to the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Syntax:

val result = operand1 and operand2

Example:

fun main() {
    val a = 5  // 0101 in binary
    val b = 3  // 0011 in binary

    val result = a and b
    println("a and b: $result")  // Output: a and b: 1 (0001 in binary)
}

2. or (Bitwise OR)

The bitwise OR operator compares each bit of its first operand to the corresponding bit of its second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Syntax:

val result = operand1 or operand2

Example:

fun main() {
    val a = 5  // 0101 in binary
    val b = 3  // 0011 in binary

    val result = a or b
    println("a or b: $result")  // Output: a or b: 7 (0111 in binary)
}

3. xor (Bitwise XOR)

The bitwise XOR operator compares each bit of its first operand to the corresponding bit of its second operand. If one of the bits is 1 (but not both), the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Syntax:

val result = operand1 xor operand2

Example:

fun main() {
    val a = 5  // 0101 in binary
    val b = 3  // 0011 in binary

    val result = a xor b
    println("a xor b: $result")  // Output: a xor b: 6 (0110 in binary)
}

4. inv (Bitwise Inversion)

The bitwise inversion operator inverts all the bits of its operand, changing 1s to 0s and 0s to 1s.

Syntax:

val result = operand.inv()

Example:

fun main() {
    val a = 5  // 0101 in binary

    val result = a.inv()
    println("a.inv(): $result")  // Output: a.inv(): -6 (all bits inverted)
}

5. shl (Left Shift)

The left shift operator shifts the bits of its first operand left by the number of positions specified by its second operand. Bits shifted out of the left side are discarded, and zeros are shifted in from the right.

Syntax:

val result = operand1 shl operand2

Example:

fun main() {
    val a = 5  // 0101 in binary

    val result = a shl 1
    println("a shl 1: $result")  // Output: a shl 1: 10 (1010 in binary)
}

6. shr (Right Shift)

The right shift operator shifts the bits of its first operand right by the number of positions specified by its second operand. Bits shifted out of the right side are discarded, and the sign bit (leftmost bit) is shifted in from the left.

Syntax:

val result = operand1 shr operand2

Example:

fun main() {
    val a = 5  // 0101 in binary

    val result = a shr 1
    println("a shr 1: $result")  // Output: a shr 1: 2 (0010 in binary)
}

7. ushr (Unsigned Right Shift)

The unsigned right shift operator shifts the bits of its first operand right by the number of positions specified by its second operand. Bits shifted out of the right side are discarded, and zeros are shifted in from the left. This operator treats the operand as an unsigned number.

Syntax:

val result = operand1 ushr operand2

Example:

fun main() {
    val a = -5  // In binary: 11111111111111111111111111111011

    val result = a ushr 1
    println("a ushr 1: $result")  // Output: a ushr 1: 2147483645
}

Example Program with Bitwise Operators

Here is an example program that demonstrates the use of various bitwise operators in Kotlin:

fun main() {
    val a = 5  // 0101 in binary
    val b = 3  // 0011 in binary

    // Bitwise AND
    println("a and b: ${a and b}")  // Output: 1 (0001 in binary)

    // Bitwise OR
    println("a or b: ${a or b}")  // Output: 7 (0111 in binary)

    // Bitwise XOR
    println("a xor b: ${a xor b}")  // Output: 6 (0110 in binary)

    // Bitwise Inversion
    println("a.inv(): ${a.inv()}")  // Output: -6

    // Left Shift
    println("a shl 1: ${a shl 1}")  // Output: 10 (1010 in binary)

    // Right Shift
    println("a shr 1: ${a shr 1}")  // Output: 2 (0010 in binary)

    // Unsigned Right Shift
    println("a ushr 1: ${a ushr 1}")  // Output: 2 (0010 in binary)
}

Conclusion

In this chapter, you learned about the various bitwise operators available in Kotlin, including bitwise AND, OR, XOR, inversion, left shift, right shift, and unsigned right shift. Understanding these operators is crucial for performing low-level bit manipulation tasks in your Kotlin programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top