How to combine 2 unsigned short into a unsigned int?

Discussion in 'Java' started by fancyerii, Nov 1, 2007.

  1. fancyerii

    fancyerii Guest

    Hi, everyone.
    The question is: I have 2 unsigned short, And I want to "concat"
    this 2 short into a unsigned ingeger value.
    If in C, it may be implemented like :
    unsigned short s1; //s1 ranges from 0 to 65535
    unsigned short s2;
    unsigned int=(s1<<16)+s2;

    But there is no unsigned in java, All integers are signed. So how
    can I achieved my goal in java ? (s1 may be very large, say
    40,000>32767)
    Thanks!
    fancyerii, Nov 1, 2007
    #1
    1. Advertising

  2. fancyerii

    instcode Guest

    On Nov 1, 10:10 am, fancyerii <> wrote:
    > Hi, everyone.
    > The question is: I have 2 unsigned short, And I want to "concat"
    > this 2 short into a unsigned ingeger value.
    > If in C, it may be implemented like :
    > unsigned short s1; //s1 ranges from 0 to 65535
    > unsigned short s2;
    > unsigned int=(s1<<16)+s2;
    >
    > But there is no unsigned in java, All integers are signed. So how
    > can I achieved my goal in java ? (s1 may be very large, say
    > 40,000>32767)
    > Thanks!


    As in JVM spec, int-type uses 4 bytes to store a signed integer on
    every JVM.

    int s1;
    int s2;

    Because s1, s2 values fall into an unsigned-short (2 bytes) range, its
    *meaning-value* is only stored in *2 lowest bytes* of a Java-integer.
    Understanding binary/hexa form of a number, we can "concat" these
    numbers as following:

    int s3 = ((s1 & 0xFFFF) << 16) | (s2 & 0xFFFF);

    Hope this helps.
    instcode, Nov 1, 2007
    #2
    1. Advertising

  3. fancyerii

    fancyerii Guest

    On 11 1 , 12 05 , instcode <> wrote:
    > On Nov 1, 10:10 am, fancyerii <> wrote:
    >
    > > Hi, everyone.
    > > The question is: I have 2 unsigned short, And I want to "concat"
    > > this 2 short into a unsigned ingeger value.
    > > If in C, it may be implemented like :
    > > unsigned short s1; //s1 ranges from 0 to 65535
    > > unsigned short s2;
    > > unsigned int=(s1<<16)+s2;

    >
    > > But there is no unsigned in java, All integers are signed. So how
    > > can I achieved my goal in java ? (s1 may be very large, say
    > > 40,000>32767)
    > > Thanks!

    >
    > As in JVM spec, int-type uses 4 bytes to store a signed integer on
    > every JVM.
    >
    > int s1;
    > int s2;
    >
    > Because s1, s2 values fall into an unsigned-short (2 bytes) range, its
    > *meaning-value* is only stored in *2 lowest bytes* of a Java-integer.
    > Understanding binary/hexa form of a number, we can "concat" these
    > numbers as following:
    >
    > int s3 = ((s1 & 0xFFFF) << 16) | (s2 & 0xFFFF);
    >
    > Hope this helps.


    Thanks.
    fancyerii, Nov 1, 2007
    #3
  4. fancyerii

    rossum Guest

    On Thu, 01 Nov 2007 04:05:09 -0000, instcode <>
    wrote:

    >On Nov 1, 10:10 am, fancyerii <> wrote:
    >> Hi, everyone.
    >> The question is: I have 2 unsigned short, And I want to "concat"
    >> this 2 short into a unsigned ingeger value.
    >> If in C, it may be implemented like :
    >> unsigned short s1; //s1 ranges from 0 to 65535
    >> unsigned short s2;
    >> unsigned int=(s1<<16)+s2;
    >>
    >> But there is no unsigned in java, All integers are signed. So how
    >> can I achieved my goal in java ? (s1 may be very large, say
    >> 40,000>32767)
    >> Thanks!

    >
    >As in JVM spec, int-type uses 4 bytes to store a signed integer on
    >every JVM.
    >
    >int s1;
    >int s2;
    >
    >Because s1, s2 values fall into an unsigned-short (2 bytes) range, its
    >*meaning-value* is only stored in *2 lowest bytes* of a Java-integer.
    >Understanding binary/hexa form of a number, we can "concat" these
    >numbers as following:
    >
    >int s3 = ((s1 & 0xFFFF) << 16) | (s2 & 0xFFFF);


    The problem you might get is that if the high bit of s3 is set then
    the result will be treated as negative.

    Just as you used a signed int to hold an unsigned short, you should
    use a signed long (8 bytes) to hold an unsigned 4 byte int, so s3
    should be a long.

    rossum

    >
    >Hope this helps.
    rossum, Nov 1, 2007
    #4
  5. fancyerii

    fancyerii Guest

    But when I run these codes, It's not the result I want.
    int i1=24785;
    int i2=42113;
    int rt;
    rt=(i1&0xFFFF)<<16+(i2&0xffff);
    The answer I got is : -1046347776 it's a negative.
    While in c the result is 1624351873.
    What's wrong?

    instcode
    > On Nov 1, 10:10 am, fancyerii <> wrote:
    > > Hi, everyone.
    > > The question is: I have 2 unsigned short, And I want to "concat"
    > > this 2 short into a unsigned ingeger value.
    > > If in C, it may be implemented like :
    > > unsigned short s1; //s1 ranges from 0 to 65535
    > > unsigned short s2;
    > > unsigned int=(s1<<16)+s2;
    > >
    > > But there is no unsigned in java, All integers are signed. So how
    > > can I achieved my goal in java ? (s1 may be very large, say
    > > 40,000>32767)
    > > Thanks!

    >
    > As in JVM spec, int-type uses 4 bytes to store a signed integer on
    > every JVM.
    >
    > int s1;
    > int s2;
    >
    > Because s1, s2 values fall into an unsigned-short (2 bytes) range, its
    > *meaning-value* is only stored in *2 lowest bytes* of a Java-integer.
    > Understanding binary/hexa form of a number, we can "concat" these
    > numbers as following:
    >
    > int s3 = ((s1 & 0xFFFF) << 16) | (s2 & 0xFFFF);
    >
    > Hope this helps.
    fancyerii, Nov 1, 2007
    #5
  6. fancyerii

    Lew Guest

    fancyerii wrote:
    > But when I run these codes, It's not the result I want.
    > int i1=24785;
    > int i2=42113;
    > int rt;
    > rt=(i1&0xFFFF)<<16+(i2&0xffff);
    > The answer I got is : -1046347776 it's a negative.
    > While in c the result is 1624351873.
    > What's wrong?


    First, that you top-posted. Please use trim-and-inline posting.

    Did you read rossum's message? Let me quote it:
    > The problem you might get is that if the high bit of s3 is set then
    > the result will be treated as negative.
    >
    > Just as you used a signed int to hold an unsigned short, you should
    > use a signed long (8 bytes) to hold an unsigned 4 byte int, so s3
    > should be a long.


    You know that Java doesn't have unsigned integer types, so why do you ask
    what's wrong? What you call "wrong" is just that Java doesn't have unsigned
    integer types.

    --
    Lew
    Lew, Nov 1, 2007
    #6
  7. fancyerii

    fancyerii Guest

    I replace int s3 by long s3, but it still got a negative .
    rossum
    > On Thu, 01 Nov 2007 04:05:09 -0000, instcode <>
    > wrote:
    >
    > >On Nov 1, 10:10 am, fancyerii <> wrote:
    > >> Hi, everyone.
    > >> The question is: I have 2 unsigned short, And I want to "concat"
    > >> this 2 short into a unsigned ingeger value.
    > >> If in C, it may be implemented like :
    > >> unsigned short s1; //s1 ranges from 0 to 65535
    > >> unsigned short s2;
    > >> unsigned int=(s1<<16)+s2;
    > >>
    > >> But there is no unsigned in java, All integers are signed. So how
    > >> can I achieved my goal in java ? (s1 may be very large, say
    > >> 40,000>32767)
    > >> Thanks!

    > >
    > >As in JVM spec, int-type uses 4 bytes to store a signed integer on
    > >every JVM.
    > >
    > >int s1;
    > >int s2;
    > >
    > >Because s1, s2 values fall into an unsigned-short (2 bytes) range, its
    > >*meaning-value* is only stored in *2 lowest bytes* of a Java-integer.
    > >Understanding binary/hexa form of a number, we can "concat" these
    > >numbers as following:
    > >
    > >int s3 = ((s1 & 0xFFFF) << 16) | (s2 & 0xFFFF);

    >
    > The problem you might get is that if the high bit of s3 is set then
    > the result will be treated as negative.
    >
    > Just as you used a signed int to hold an unsigned short, you should
    > use a signed long (8 bytes) to hold an unsigned 4 byte int, so s3
    > should be a long.
    >
    > rossum
    >
    > >
    > >Hope this helps.
    fancyerii, Nov 1, 2007
    #7
  8. fancyerii

    Lew Guest

    fancyerii wrote:
    > I replace int s3 by long s3, but it still got a negative .


    Please do not top-post. Use trim-and-inline posting.

    instcode wrote:
    >>> int s3 = ((s1 & 0xFFFF) << 16) | (s2 & 0xFFFF);


    See all that masking going on to make a short look unsigned as an int?

    Envision all that masking going on to make an int look unsigned as a long.

    It sounds like you need to read up on:
    <http://java.sun.com/docs/books/tutorial/index.html>
    particularly
    <http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html>
    and
    <http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html>

    --
    Lew
    Lew, Nov 1, 2007
    #8
  9. fancyerii wrote:
    > But when I run these codes, It's not the result I want.
    > int i1=24785;
    > int i2=42113;
    > int rt;
    > rt=(i1&0xFFFF)<<16+(i2&0xffff);
    > The answer I got is : -1046347776 it's a negative.
    > While in c the result is 1624351873.
    > What's wrong?


    A couple of things. First of all, you are missing some parentheses:

    rt=((i1&0xFFFF)<<16)+(i2&0xffff);

    Although it is not necessary in this case, because the leading bit of i1
    is zero, in general you need to print it as the low order 32 bits of a
    long to avoid negative output:

    System.out.println(rt & 0xffffffffL);

    May I ask why you are doing all this? Generally, a short[2] is a more
    convenient, less fiddly, representation of a pair of short values.

    Patricia
    Patricia Shanahan, Nov 1, 2007
    #9
  10. fancyerii

    fancyerii Guest

    Lew
    > fancyerii wrote:
    > > But when I run these codes, It's not the result I want.
    > > int i1=24785;
    > > int i2=42113;
    > > int rt;
    > > rt=(i1&0xFFFF)<<16+(i2&0xffff);
    > > The answer I got is : -1046347776 it's a negative.
    > > While in c the result is 1624351873.
    > > What's wrong?

    >
    > First, that you top-posted. Please use trim-and-inline posting.
    >
    > Did you read rossum's message? Let me quote it:
    > > The problem you might get is that if the high bit of s3 is set then
    > > the result will be treated as negative.
    > >
    > > Just as you used a signed int to hold an unsigned short, you should
    > > use a signed long (8 bytes) to hold an unsigned 4 byte int, so s3
    > > should be a long.

    >
    > You know that Java doesn't have unsigned integer types, so why do you ask
    > what's wrong? What you call "wrong" is just that Java doesn't have unsigned
    > integer types.
    >
    > --
    > Lew


    Sorry, I'm not familiar with the google group.
    fancyerii, Nov 1, 2007
    #10
  11. fancyerii

    fancyerii Guest

    Patricia Shanahan
    > fancyerii wrote:
    > > But when I run these codes, It's not the result I want.
    > > int i1=24785;
    > > int i2=42113;
    > > int rt;
    > > rt=(i1&0xFFFF)<<16+(i2&0xffff);
    > > The answer I got is : -1046347776 it's a negative.
    > > While in c the result is 1624351873.
    > > What's wrong?

    >
    > A couple of things. First of all, you are missing some parentheses:
    >
    > rt=((i1&0xFFFF)<<16)+(i2&0xffff);
    >
    > Although it is not necessary in this case, because the leading bit of i1
    > is zero, in general you need to print it as the low order 32 bits of a
    > long to avoid negative output:
    >
    > System.out.println(rt & 0xffffffffL);
    >
    > May I ask why you are doing all this? Generally, a short[2] is a more
    > convenient, less fiddly, representation of a pair of short values.
    >
    > Patricia


    I want to mapping bigram(2 words sequence) into a number.
    I have a dictionary whose size is about 50,000. So I give each word a
    number.
    And for a bigram <w1,w2>, I want to mapping it into a number.
    So I want to use w1 as the higher 16 bits and w2 as the lower 16 bits.
    fancyerii, Nov 1, 2007
    #11
  12. fancyerii

    Lew Guest

    fancyerii wrote:
    > Sorry, I'm not familiar with the google group.


    Google group?

    --
    Lew
    Lew, Nov 1, 2007
    #12
  13. fancyerii

    Lew Guest

    Lew wrote:
    > fancyerii wrote:
    >> Sorry, I'm not familiar with the google group.

    >
    > Google group?


    I ask because I don't use Google Groups, so I don't know what you're talking
    about.

    --
    Lew
    Lew, Nov 1, 2007
    #13
  14. fancyerii

    rossum Guest

    On Thu, 01 Nov 2007 06:10:17 -0700, fancyerii <>
    wrote:


    >> On Nov 1, 10:10 am, fancyerii <> wrote:
    >> > Hi, everyone.
    >> > The question is: I have 2 unsigned short, And I want to "concat"
    >> > this 2 short into a unsigned ingeger value.
    >> > If in C, it may be implemented like :
    >> > unsigned short s1; //s1 ranges from 0 to 65535
    >> > unsigned short s2;
    >> > unsigned int=(s1<<16)+s2;
    >> >
    >> > But there is no unsigned in java, All integers are signed. So how
    >> > can I achieved my goal in java ? (s1 may be very large, say
    >> > 40,000>32767)
    >> > Thanks!

    >>
    >> As in JVM spec, int-type uses 4 bytes to store a signed integer on
    >> every JVM.
    >>
    >> int s1;
    >> int s2;
    >>
    >> Because s1, s2 values fall into an unsigned-short (2 bytes) range, its
    >> *meaning-value* is only stored in *2 lowest bytes* of a Java-integer.
    >> Understanding binary/hexa form of a number, we can "concat" these
    >> numbers as following:
    >>
    >> int s3 = ((s1 & 0xFFFF) << 16) | (s2 & 0xFFFF);
    >>
    >> Hope this helps.


    [Top posting modified.]

    >But when I run these codes, It's not the result I want.
    > int i1=24785;
    > int i2=42113;
    > int rt;

    You should declare rt as a long, not an int. As an int rt will only
    hold 31 unsigned bits, not 32. A long will hold up to 63 unsigned
    bits.

    > rt=(i1&0xFFFF)<<16+(i2&0xffff);

    As Patricia has pointed out you are missing a pair of brackets:

    rt = ((i1 & 0xFFFF) << 16) + (i2 & 0xffff);
    ^ ^
    I would also be inclined to make at least one of the operands a long,
    just to be sure that the addition is done in 64 bits, not 32:

    result = ((i1 & 0xFFFF) << 16) + (long)(i2 & 0xFFFF);

    >The answer I got is : -1046347776 it's a negative.
    >While in c the result is 1624351873.
    >What's wrong?

    Look at the bit pattern used to represent both numbers. Pay especial
    attention to the most significant bit of the pattern. Now look up
    what role the most significant bit plays in a 32 bit Java integer.

    rossum
    rossum, Nov 1, 2007
    #14
  15. rossum wrote:
    ....
    > I would also be inclined to make at least one of the operands a long,
    > just to be sure that the addition is done in 64 bits, not 32:
    >
    > result = ((i1 & 0xFFFF) << 16) + (long)(i2 & 0xFFFF);


    It is fine to the addition in 32 bits. No need to make the calculation
    long. The result will be the same as the low order 32 bits that would
    have resulted if it had been done as long.

    The differences between unsigned and 2's complement signed lie in other
    areas, such as comparison and conversion results.

    Patricia
    Patricia Shanahan, Nov 1, 2007
    #15
  16. fancyerii wrote:
    ....
    > I want to mapping bigram(2 words sequence) into a number.
    > I have a dictionary whose size is about 50,000. So I give each word a
    > number.
    > And for a bigram <w1,w2>, I want to mapping it into a number.


    I assume you have too many bigrams to spare an extra 4 bytes each to use
    a pair of references, instead of the int? Beware of using long to get
    unsignedness - it will cost you the space you are saving by not using
    references.

    > So I want to use w1 as the higher 16 bits and w2 as the lower 16 bits.
    >


    Why do the numbers have to be unsigned?

    Patricia
    Patricia Shanahan, Nov 1, 2007
    #16
  17. fancyerii

    Roedy Green Guest

    On Wed, 31 Oct 2007 20:10:02 -0700, fancyerii <>
    wrote, quoted or indirectly quoted someone who said :

    >
    > But there is no unsigned in java, All integers are signed. So how
    >can I achieved my goal in java ? (s1 may be very large, say
    >40,000>32767)


    see http://mindprod.com/jgloss/unsigned.html

    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Nov 2, 2007
    #17
  18. fancyerii

    Roedy Green Guest

    On Thu, 01 Nov 2007 06:10:17 -0700, fancyerii <>
    wrote, quoted or indirectly quoted someone who said :

    >The answer I got is : -1046347776 it's a negative.
    >While in c the result is 1624351873.
    >What's wrong?


    Java does not support unsigned. You used a signed printing routine.
    To see it as C does, you would have to use an unsigned printing
    routine. The bits are the same for both. You would have to write your
    own unsignedToString method. You would write a very simple one by
    masking off the high 32 bits after a conversion to long, then a
    Long.toString.

    see http://mindprod.com/jgloss/unsigned.html
    for the code.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Nov 4, 2007
    #18
  19. fancyerii

    Roedy Green Guest

    On Thu, 01 Nov 2007 06:45:07 -0700, fancyerii <>
    wrote, quoted or indirectly quoted someone who said :

    >Sorry, I'm not familiar with the google group.


    see http://mindprod.com/jgloss/newsgroups.html

    Google is one of many ways of accessing this discussion, but they
    don't own or control it. People get touchy about that since Google
    sometimes acts as if it did
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Nov 4, 2007
    #19
  20. fancyerii

    Roedy Green Guest

    On Thu, 01 Nov 2007 10:01:22 -0400, Lew <> wrote,
    quoted or indirectly quoted someone who said :

    >I ask because I don't use Google Groups, so I don't know what you're talking
    >about.


    Liar liar. You are just hazing the newbies again.

    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Nov 4, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S300
    Replies:
    4
    Views:
    21,963
    Roedy Green
    Aug 19, 2003
  2. Timo Freiberger
    Replies:
    3
    Views:
    923
    Bob Hairgrove
    Oct 30, 2004
  3. er
    Replies:
    6
    Views:
    474
    Andre Kostur
    Sep 14, 2007
  4. ciccio

    int*unsigned int = unsigned?

    ciccio, Jun 4, 2010, in forum: C++
    Replies:
    2
    Views:
    390
    Öö Tiib
    Jun 4, 2010
  5. pozz
    Replies:
    12
    Views:
    718
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page