> Hi, everyone.
> The question is: I have 2 unsigned short, And I want to "concat"
[quoted text clipped - 8 lines]
> 40,000>32767)
> Thanks!
> > Hi, everyone.
> > The question is: I have 2 unsigned short, And I want to "concat"
[quoted text clipped - 23 lines]
>
> Hope this helps.
Thanks.
>> Hi, everyone.
>> The question is: I have 2 unsigned short, And I want to "concat"
[quoted text clipped - 21 lines]
>
>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 - 01 Nov 2007 14:16 GMT
I replace int s3 by long s3, but it still got a negative .
rossum
> >> Hi, everyone.
> >> The question is: I have 2 unsigned short, And I want to "concat"
[quoted text clipped - 32 lines]
>
> >Hope this helps.
Lew - 01 Nov 2007 14:20 GMT
> 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>

Signature
Lew
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
> > Hi, everyone.
> > The question is: I have 2 unsigned short, And I want to "concat"
[quoted text clipped - 23 lines]
>
> Hope this helps.
Lew - 01 Nov 2007 14:15 GMT
> But when I run these codes, It's not the result I want.
> int i1=24785;
[quoted text clipped - 4 lines]
> 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.

Signature
Lew
fancyerii - 01 Nov 2007 14:45 GMT
Lew
> > But when I run these codes, It's not the result I want.
> > int i1=24785;
[quoted text clipped - 21 lines]
> --
> Lew
Sorry, I'm not familiar with the google group.
Lew - 01 Nov 2007 14:59 GMT
> Sorry, I'm not familiar with the google group.
Google group?

Signature
Lew
Lew - 01 Nov 2007 15:01 GMT
>> 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.

Signature
Lew
Roedy Green - 04 Nov 2007 21:48 GMT
>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.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lew - 04 Nov 2007 22:47 GMT
>> 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.
Plonk.

Signature
Lew
Roedy Green - 05 Nov 2007 21:33 GMT
>> Liar liar. You are just hazing the newbies again.
>
>Plonk.
You know perfectly well what Google groups are. You are a regular
denizen. People explain the difference between Google Groups and
newsgroups to newbies all the time. You were being disingenuous
pretending not to understand what the newbie was asking. You were just
looking for an excuse to berate him.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 04 Nov 2007 21:47 GMT
>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

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Patricia Shanahan - 01 Nov 2007 14:37 GMT
> But when I run these codes, It's not the result I want.
> int i1=24785;
[quoted text clipped - 4 lines]
> 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
fancyerii - 01 Nov 2007 14:51 GMT
Patricia Shanahan
> > But when I run these codes, It's not the result I want.
> > int i1=24785;
[quoted text clipped - 19 lines]
>
> 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.
Patricia Shanahan - 02 Nov 2007 00:12 GMT
...
> 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
rossum - 01 Nov 2007 21:16 GMT
>> > Hi, everyone.
>> > The question is: I have 2 unsigned short, And I want to "concat"
[quoted text clipped - 23 lines]
>>
>> 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
Patricia Shanahan - 01 Nov 2007 21:23 GMT
...
> 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
Roedy Green - 04 Nov 2007 21:46 GMT
>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.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com