unsigned shift confusion

M

Mike

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.
 
P

Patricia Shanahan

Mike said:
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
 
M

Mike

Patricia said:
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"
 
P

Patricia Shanahan

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
 
J

Jacques-Olivier Haenni

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

Patricia said:
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"
 
M

Mike

Patricia said:
Mike wrote:
...

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.
 
M

Mike

Jacques-Olivier Haenni said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top