Why does the right-shift operator produce a zero instead of a one?
- by mrt181
Hi, i am teaching myself java and i work through the exercises in Thinking in Java.
On page 116, exercise 11, you should right-shift an integer through all its binary positions and display each position with 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 looks like this:
1000
1100
1110
1111
When i run this code i get this:
1000
100
10
1
What is going on here. Are the digits cut off?
I am using jdk1.6.0_20 64bit. The book uses jdk1.5 32bit.