Scala - Bitwise Operators Example

1. Introduction

Bitwise operators in Scala are used to perform manipulation of individual bits of a number. They can be used with any of the integral types (Byte, Short, Int, Long). These operators are often used in low-level programming, such as graphics programming and device driver creation. This blog post will explain Scala's bitwise operators and provide examples demonstrating their use.

Scala - Bitwise Operators

Scala supports several bitwise operators:

& (bitwise AND): Compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1.

| (bitwise OR): Compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1.

^ (bitwise XOR): Compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different, the corresponding result bit is set to 1.

~ (bitwise complement): Inverts all the bits of the operand.

<< (left shift): Shifts the bits of the first operand to the left by the number of positions specified by the second operand. Zeros are shifted into the low-order positions.

>> (right shift): Shifts the bits of the first operand to the right by the number of positions specified by the second operand. The sign bit is used to fill the high-order positions.

>>> (unsigned right shift): Shifts the bits of the first operand to the right by the number of positions specified by the second operand. Zeros are shifted into the high-order positions.

2. Program Steps

1. Define integer variables for bitwise manipulation.

2. Apply each of the bitwise operators to these variables.

3. Print the results of the operations.

4. Execute the program to verify the outcomes.

3. Code Program

object BitwiseOperatorsDemo extends App {
  val a: Int = 60       // 0011 1100 in binary
  val b: Int = 13       // 0000 1101 in binary

  // Bitwise AND
  val andResult: Int = a & b  // 0000 1100
  // Bitwise OR
  val orResult: Int = a | b   // 0011 1101
  // Bitwise XOR
  val xorResult: Int = a ^ b  // 0011 0001
  // Bitwise Complement
  val complementResult: Int = ~a // 1100 0011 (in 32-bit representation)
  // Left shift
  val leftShift: Int = a << 2    // 1111 0000
  // Right shift
  val rightShift: Int = a >> 2   // 0000 1111
  // Unsigned right shift
  val unsignedRightShift: Int = a >>> 2 // 0000 1111

  // Print results
  println(s"Bitwise AND of $a and $b = $andResult")
  println(s"Bitwise OR of $a and $b = $orResult")
  println(s"Bitwise XOR of $a and $b = $xorResult")
  println(s"Bitwise Complement of $a = $complementResult")
  println(s"Left shift of $a by 2 = $leftShift")
  println(s"Right shift of $a by 2 = $rightShift")
  println(s"Unsigned right shift of $a by 2 = $unsignedRightShift")
}

Output:

Bitwise AND of 60 and 13 = 12
Bitwise OR of 60 and 13 = 61
Bitwise XOR of 60 and 13 = 49
Bitwise Complement of 60 = -61
Left shift of 60 by 2 = 240
Right shift of 60 by 2 = 15
Unsigned right shift of 60 by 2 = 15

Explanation:

1. We have two integers a and b with binary representations 0011 1100 and 0000 1101, respectively.

2. The & operator performs a bitwise AND operation, where the result has bits set to 1 only in positions where both a and b have a 1 bit.

3. The | operator performs a bitwise OR operation, where the result has bits set to 1 in positions where a or b (or both) have a 1 bit.

4. The ^ operator performs a bitwise XOR operation, where the result has bits set to 1 in positions where a


Comments