> Hi, I'm testing the unsigned shift operator in Java. Unfortunately, the
> value I predicted for the output is 2^17 (131072), not 2^17 - 1
> (131071). See below:
...
>> Hi, I'm testing the unsigned shift operator in Java. Unfortunately,
>> the value I predicted for the output is 2^17 (131072), not 2^17 - 1
[quoted text clipped - 6 lines]
>
> Patricia
Hi Patricia. Maybe. But I figured Java (my version is 1.5.0_09) uses
twos complement to store negative numbers? Here's a different set of
code with different output:
Code:
"""
class unsignedshift {
public static void main(String args[])
{
String bs1, bs2;
int a = -10;
bs1 = Integer.toBinaryString(a);
a = a >>> 15;
bs2 = Integer.toBinaryString(a);
System.out.println("The original -10 in binary is: " + bs1);
System.out.println("a (in binary) = " + bs2);
System.out.println("a = " + a);
}
}
"""
Output:
"""
The original -10 in binary is: 11111111111111111111111111110110
a (in binary) = 11111111111111111
a = 131071
"""
The original -10 in binary is right if you consider twos complement. And
I even get the right set of bits (17) when shifting 15 places to the
right. But 2^17 = 131072, which doesn't coincide with the output "a =
131071"

Signature
Mike
Patricia Shanahan - 29 Mar 2007 15:25 GMT
...
> Output:
> """
[quoted text clipped - 7 lines]
> right. But 2^17 = 131072, which doesn't coincide with the output "a =
> 131071"
Do you think 11111111111111111 represents 2^17? If so, why?
Patricia
Mike - 29 Mar 2007 15:45 GMT
> ...
>> Output:
[quoted text clipped - 12 lines]
>
> Patricia
The first power is 2^0, so it's not 2^17 -- my fault. Jacques put it
into perspective. Thanks for your help.

Signature
Mike
Jacques-Olivier Haenni - 29 Mar 2007 15:29 GMT
Hi,
Is it due to the way you are converting the binary value into a decimal
one ?
For example, 0...000111 is not 2^3.
0...000111 = 2^0 + 2^1 + 2^2 = 7 = 2^3 - 1
In the same way, 17 bits set to 1 means 2^17-1.
I hope I've not missed something in your question...
Jacques-Olivier
>>> Hi, I'm testing the unsigned shift operator in Java. Unfortunately,
>>> the value I predicted for the output is 2^17 (131072), not 2^17 - 1
[quoted text clipped - 41 lines]
> the right. But 2^17 = 131072, which doesn't coincide with the output
> "a = 131071"
Mike - 29 Mar 2007 15:47 GMT
> Hi,
>
[quoted text clipped - 9 lines]
>
> Jacques-Olivier
This clarifies my confusion. I always knew the max. decimal number that
can be represented in 32 bits is 2^31 -1 (assuming signed number). So,
since there's still 17 bits it's really 2^17 - 1 which is what you
described. Thanks!

Signature
Mike