unsigned shift confusion  
Author Message
Mike





PostPosted: 2007-3-29 21:29:00 Top

java-programmer, unsigned shift confusion 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:

"""
class unsignedshift {
public static void main(String args[])
{
int a = -10;
String bs1;
a = a >>> 15;
bs1 = Integer.toBinaryString(a);
System.out.println(bs1);
System.out.println("The length of the binary string is " + bs1.length());
System.out.println("a = " + a);
}
}

"""

Output is:

"""
11111111111111111
The length of the binary string is 17
a = 131071
"""

Where is the difference of one coming from?

Thanks.


--
Mike
 
Patricia Shanahan





PostPosted: 2007-3-29 21:52:00 Top

java-programmer >> unsigned shift confusion Mike wrote:
> 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:
...
> Where is the difference of one coming from?

Maybe you were assuming sign-and-magnitude representation of -10,
0x1000000a, not 2's complement, 0xfffffff6?

Patricia
 
Mike





PostPosted: 2007-3-29 22:11:00 Top

java-programmer >> unsigned shift confusion Patricia Shanahan wrote:
> Mike wrote:
>> 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:
> ...
>> Where is the difference of one coming from?
>
> Maybe you were assuming sign-and-magnitude representation of -10,
> 0x1000000a, not 2's complement, 0xfffffff6?
>
> 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"

--
Mike
 
 
Patricia Shanahan





PostPosted: 2007-3-29 22:26:00 Top

java-programmer >> unsigned shift confusion Mike wrote:
...
> 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"

Do you think 11111111111111111 represents 2^17? If so, why?

Patricia

 
 
Jacques-Olivier Haenni





PostPosted: 2007-3-29 22:30:00 Top

java-programmer >> unsigned shift confusion 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


Mike wrote:
> Patricia Shanahan wrote:
>> Mike wrote:
>>> 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:
>> ...
>>> Where is the difference of one coming from?
>>
>> Maybe you were assuming sign-and-magnitude representation of -10,
>> 0x1000000a, not 2's complement, 0xfffffff6?
>>
>> 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"
>
 
 
Mike





PostPosted: 2007-3-29 22:45:00 Top

java-programmer >> unsigned shift confusion Patricia Shanahan wrote:
> Mike wrote:
> ...
>> 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"
>
> Do you think 11111111111111111 represents 2^17? If so, why?
>
> Patricia
>

The first power is 2^0, so it's not 2^17 -- my fault. Jacques put it
into perspective. Thanks for your help.

--
Mike
 
 
Mike





PostPosted: 2007-3-29 22:47:00 Top

java-programmer >> unsigned shift confusion Jacques-Olivier Haenni wrote:
> 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

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!

--
Mike