Page 168 - ComputerScience_Class_11
P. 168
The output of the preceding program is as follows:
BlueJ: Terminal Window - Java
Options
Enter 1st number
5
Enter 2nd number
4
Using different bitwise operator, we get the following result:
a & b= 4
a | b = 5
~a = -6
a ^ b = 1
Shift Operator
This bitwise operator is used to shift bits of the first operand to right or left one by one, thus performing bit manipulation
on data.
There are three types of Shift Operators in Java.
1. Bitwise signed left shift operator (<<)
2. Bitwise signed right shift operator (>>)
3. Bitwise unsigned right shift operator (>>>)
Let us see in detail.
1. 15 << 2
Converting 15 to binary, we get : 1111
Shifting value to left by 2 bits, we get : 111100
Converting the binary value into decimal value after applying the shift operator, we get: 60
So, 15 << 2 is 60.
2. 40 << 1
Converting 40 to binary, we get : 101000
Shifting value to left by 1 bit, we get : 1010000
Converting the binary value into decimal value after applying the shift operator, we get: 80
So, 40 << 1 is 80.
3. 125 >> 3
Converting 125 to binary, we get : 1111101
Shifting value to left by 3 bits, we get : 1111
Converting the binary value into decimal value after applying the shift operator, we get: 15
So, 125 >> 3 is 15.
4. 46 >> 2
Converting a to binary, we get : 101110
Shifting value to the left by 3 bits, we get : 1011
Converting the binary value into decimal value after applying the shift operator, we get: 11
So, 46 >> 2 is 11.
5. 14 >>> 2
Converting a to binary, we get : 1110
Shifting value to the left by 3 bits, we get : 0011
Converting the binary value into decimal value after applying the shift operator, we get: 3
So, 14 >>> 2 is 3.
166 Touchpad Computer Science (Ver. 3.0)-XI

