Page 344 - Computer_Science_F5
P. 344
Output: Chapter Five: Object oriented programming with Java
FOR ONLINE READING ONLY
Figure 5.10: Bitwise operators
Example of Bitwise right shift and Bitwise left shift
(i) Perform the bitwise left shift operation on the integer 6. Left shift each bit
by 1. Write the answer in binary and decimal.
(ii) Perform the right shift by two bits operation on the integer 7 without use of
Java program.
(iii) Perform the right shift by two bits operation on integer 60 without use of
Java program.
Solution:
(i) 6=0110
6<<1=1100 in Binary; 6<<1=12 in Decimal
Program Example 5.9:
Java program to perform left shift by one bit operations
class Bits {
public static void main(String[] args) {
int number = 6;
// 1 bit left shift
System.out.println(“Number left shifted by 1 bit: “ + (number <<1)); // prints 12
}
}
335
Student’s Book Form Five
Computer Science Form 5.indd 335 23/07/2024 12:34

