byte x = -1;
for(int i = 0; i < 8; i++)
{
x = (byte) (x >>> 1);
System.out.println("X: " + x);
}
As I understand it, java stores data in two's-complement, meaning -1 = 11111111 (according to wikipedia).
Also, from the java docs: "The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension. "
Which means that >>> would shift a 0 to the left most bit every time. So I expect this code to be
iteration: bit representation of x
0: 11111111
1: 01111111
2: 00111111
3: 00011111
...so on
However, my output is always X: -1, meaning (I guess) that >>> is putting the sign bit in the left most position. So I then try >>, and same result.
What's going on? I would expect my output to be: X: -1, x: 127, x: 63, etc.
Please help.
jbu