Table:
The bitwise operators
& |
bitwise AND |
|
| |
bitwise inclusive OR |
|
^ |
bitwise exclusive OR |
|
<< |
left shift |
|
>> |
right shift with sign
extension |
|
>>> |
right shift with
zero extension |
|
~ |
one's
complement |
|
|
In this exercise, you'll be guided through manipulating an integer as
a bit string, and in how to extract arbitrary fields from the string.
Finally, we'll cover how to convert arbitrary length bit strings in
signed numerical values.
- Complete the following application that takes a 32 bit signed
integer and prints it out as a bit string. Your task is to complete
the toString method to print out in a binary representation. Obtain
the integer value from arguments to the program.
class BitString {
private int value; // The 32 bit value of this bitstring
public BitString(int value) {
this.value = value;
}
public int getValue() {
return(this.value);
}
public void setValue(int toValue) {
this.value = toValue;
}
public String toString() {}
public static void main(String args[]) {
int testValue = 15;
if(args.length > 0) {
testValue = Integer.parseInt(args[0]);
} // Could extend this to get args[1] or args[2] for further input
// Do something with testValue
}
}
- Now extend the class to ask the user for a start point and a
width of field to print out. Print the binary representation of
those fields.
- Finally modify the code to print out both the signed and
unsigned values of the extracted fields as decimal numbers.
Ian Wakeman
2005-02-22