I teach myself java and I work on thinking exercises in Java.
On page 116, exercise 11, you must shift the integer over all of your binary positions and display each position using Integer.toBinaryString.
public static void main(String[] args) {
int i = 8;
System.out.println(Integer.toBinaryString(i));
int maxIterations = Integer.toBinaryString(i).length();
int j;
for (j = 1; j < maxIterations; j++) {
i >>= 1;
System.out.println(Integer.toBinaryString(i));
}
In the solution guide, the output is as follows:
1000
1100
1110
1111
When I run this code, I get the following:
1000
100
10
1
What's going on here. Are the numbers cut off?
I am using jdk1.6.0_20 64bit. The book uses jdk1.5 32bit.
source
share