We have 5 bitwise operators and each of which has its own functionality. In this post, we are going to see the different types of bitwise operators and how we can implement them.
Table of Contents
- demonstrate the bitwise left-shift (<<) operator
- demonstrate the bitwise right-shift (<<) operator
- demonstrate the Bitwise AND (&) Operator
- demonstrate the Bitwise OR (|) Operator
- demonstrate the Bitwise XOR (^) Operator
demonstrate the bitwise left-shift (<<) operator
object divineseo{
def main(args: Array[String]){
var number : Int = 0
var result : Int = 0
var left_shift : Int = 0
Console.println("Enter the number : ")
number = scala.io.StdIn.readInt()
Console.println("How many bit you want to left-shift : ")
left_shift = scala.io.StdIn.readInt()
result = number << left_shift
println("Result is : "+result)
}
}
Scala
Output
Enter the number : 9
How many bit you want to left-shift : 2
Result is : 36
How it is actually done?
result = number << left_shift
result = 9 * (22)
result = 9 * 4
result = 36
How left-shift is performed?
First, our number is converted into binary numbers and then we left-shift the data.
Let number is 9
we have to left shift the number 2 times
First we have to convert the data into 1 byte (8 bits) binary data
9 = 0000 1001
as mentioned above we have to shift it 2 times
1st time = 0001 0010
2nd time= 0010 0100 = 36
demonstrate the bitwise right-shift (<<) operator
object divineseo{
def main(args: Array[String]){
var number : Int = 0
var result : Int = 0
var right_shift : Int = 0
Console.println("Enter the number : ")
number = scala.io.StdIn.readInt()
Console.println("How many bit you want to right-shift : ")
right_shift = scala.io.StdIn.readInt()
result = number >> right_shift
println("Result is : "+result)
}
}
Scala
Output
Enter the number : 9
How many bit you want to right-shift : 2
Result is : 2
How it is actually done?
result = number >> right_shift
result = 9 / (22)
result = 9 / 4
result = 2
demonstrate the Bitwise AND (&) Operator
object divineseo{
def main(args: Array[String]){
var number : Int = 0
var result : Int = 0
var number_1 : Int = 0
Console.println("Enter 1st number : ")
number = scala.io.StdIn.readInt()
Console.println("Enter 2nd number : ")
number_1 = scala.io.StdIn.readInt()
result = number & number_1
println("Result is : "+result)
}
}
Scala
Output
Enter 1st number : 7
Enter 2nd number : 5
Result is : 5
Explanation
result = number & number_1
result = 7 & 5
The binary equivalent of 7 is 0111
The binary equivalent of 5 is 0101.
Then
0111
0101
-----
0101
That is 5.
For this you have to remember the truth table of AND gate
A | B | Y
--------
0 | 0 | 0
--------
0 | 1 | 0
--------
1 | 0 | 0
--------
1 | 1 | 1
--------
demonstrate the Bitwise OR (|) Operator
object divineseo{
def main(args: Array[String]){
var number : Int = 0
var result : Int = 0
var number_1 : Int = 0
Console.println("Enter 1st number : ")
number = scala.io.StdIn.readInt()
Console.println("Enter 2nd number : ")
number_1 = scala.io.StdIn.readInt()
result = number | number_1
println("Result is : "+result)
}
}
Scala
Output
Enter 1st number : 7
Enter 2nd number : 5
Result is : 7
Explanation
result = number | number_1
result = 7 | 5
The binary equivalent of 7 is 0111
The binary equivalent of 5 is 0101.
Then
0111
0101
-----
0111
That is 7.
For this you have to remember the truth table of OR gate
A | B | Y
--------
0 | 0 | 0
--------
0 | 1 | 1
--------
1 | 0 | 1
--------
1 | 1 | 1
--------
demonstrate the Bitwise XOR (^) Operator
object divineseo{
def main(args: Array[String]){
var number : Int = 0
var result : Int = 0
var number_1 : Int = 0
Console.println("Enter 1st number : ")
number = scala.io.StdIn.readInt()
Console.println("Enter 2nd number : ")
number_1 = scala.io.StdIn.readInt()
result = number ^ number_1
println("Result is : "+result)
}
}
Scala
Output
Enter 1st number : 7
Enter 2nd number : 5
Result is : 2
Explanation
result = number ^ number_1
result = 7 ^ 5
The binary equivalent of 7 is 0111
The binary equivalent of 5 is 0101.
Then
0111
0101
-----
0010
That is 2.
For this you have to remember the truth table of XOR gate
A | B | Y
--------
0 | 0 | 0
--------
0 | 1 | 1
--------
1 | 0 | 1
--------
1 | 1 | 0
--------