Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

unsigned shift confusion

Thread view: 
Mike - 29 Mar 2007 14:29 GMT
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.

Signature

Mike

Patricia Shanahan - 29 Mar 2007 14:52 GMT
> 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 - 29 Mar 2007 15:11 GMT
>> 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.