Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to combine 2 unsigned short into a unsigned int?

Reply
Thread Tools

How to combine 2 unsigned short into a unsigned int?

 
 
fancyerii
Guest
Posts: n/a
 
      11-01-2007

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.

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      11-01-2007
fancyerii wrote:
> Sorry, I'm not familiar with the google group.


Google group?

--
Lew
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      11-01-2007
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
 
Reply With Quote
 
rossum
Guest
Posts: n/a
 
      11-01-2007
On Thu, 01 Nov 2007 06:10:17 -0700, fancyerii <>
wrote:


>> On Nov 1, 10:10 am, fancyerii <fancye...@gmail.com> 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

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-01-2007
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
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-01-2007
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-02-2007
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-04-2007
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-04-2007
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-04-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
unsigned short, short literals Ioannis Vranos C Programming 5 03-05-2008 01:25 AM
unsigned short short? slougheed@gmail.com C++ 4 10-16-2006 11:25 PM
Stuffing LSBs of long into unsigned short Fred Ma C Programming 6 11-21-2003 03:47 AM



Advertisments