Why does the right-shift operator produce a zero instead of a one?
Posted
by mrt181
on Stack Overflow
See other posts from Stack Overflow
or by mrt181
Published on 2010-06-09T16:35:15Z
Indexed on
2010/06/09
16:42 UTC
Read the original article
Hit count: 198
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.
© Stack Overflow or respective owner