Java Bitwise Operators - Exercise
Objective:Use bitwise operators to store two short values in an int
variable.
Scoring:
This exercise is worth 10 points.
Instructions
Write a class called Bitwise
consisting of a single instance variable of type int
and instance methods putLow()
, putHigh()
, getLow()
, and getHigh()
.
The putLow()
method should accept a parameter of type short
and store this value in the low order 16 bits of the int
instance variable. The putHigh()
method should also accept a parameter of type short
, but in this case, the value should be stored in the high order 16 bits of the int
instance variable.
The getLow()
and getHigh()
methods should return the short values that were stored by the putLow()
and putHigh()
methods respectively. To implement these instance methods it is recommended that you use the >>,<<, & and | operators.
You will also need to use the (short)
cast operator in your getLow()
and getHigh()
methods.
Here is a test application you can use to verify that your Bitwise
class is working correctly: