- **Java**
(*http://www.velocityreviews.com/forums/f30-java.html*)

- - **How to combine 2 unsigned short into a unsigned int?**
(*http://www.velocityreviews.com/forums/t548525-how-to-combine-2-unsigned-short-into-a-unsigned-int.html*)

How to combine 2 unsigned short into a unsigned int?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! |

Re: How to combine 2 unsigned short into a unsigned int?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. |

Re: How to combine 2 unsigned short into a unsigned int?On 11 1 , 12 05 , instcode <ngxk...@gmail.com> 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. Thanks. |

Re: How to combine 2 unsigned short into a unsigned int?On Thu, 01 Nov 2007 04:05:09 -0000, instcode <ngxkhoa@gmail.com>
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); 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. |

Re: How to combine 2 unsigned short into a unsigned int?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 <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. |

Re: How to combine 2 unsigned short into a unsigned int?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 |

Re: How to combine 2 unsigned short into a unsigned int?I replace int s3 by long s3, but it still got a negative .
rossum > On Thu, 01 Nov 2007 04:05:09 -0000, instcode <ngxkhoa@gmail.com> > 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); > > 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. |

Re: How to combine 2 unsigned short into a unsigned int?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 |

Re: How to combine 2 unsigned short into a unsigned int?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 |

Re: How to combine 2 unsigned short into a unsigned int?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. |

All times are GMT. The time now is 08:35 AM. |

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.

SEO by vBSEO ©2010, Crawlability, Inc.