yangxu998 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

UnsignedShorts #670

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Please provide the same functionality as UnsignedBytes, but for shorts. My 
specific use-case is calculating CRC-16 values.

Original issue reported on code.google.com by cow...@bbs.darktech.org on 26 Jul 2011 at 2:32

GoogleCodeExporter commented 9 years ago
How much mileage could you get out of simply using int? What are the edge cases 
this would leave uncovered?

Original comment by fry@google.com on 28 Jul 2011 at 5:36

GoogleCodeExporter commented 9 years ago
You can't always use an int. In my case, I'm given a ByteBuffer full of bytes 
read from a serial port. I need to read a 16-bit unsigned short from the 
buffer. The only two methods available to me are ByteBuffer.getShort() and 
ByteBuffer.get(byte[]).

If I had UnsignedShorts I'd simply invoke: 
UnsignedShorts.toInt(ByteBuffer.getShort())

Original comment by cow...@bbs.darktech.org on 8 Aug 2011 at 6:34

GoogleCodeExporter commented 9 years ago
Here is a quick attempt at transforming UnsignedBytes to UnsignedShorts. I 
replaced "byte" by "short" and 127 with 65535, etc.

Original comment by cow...@bbs.darktech.org on 8 Aug 2011 at 7:13

Attachments:

GoogleCodeExporter commented 9 years ago
I've been using the proposed patch for over a month now without any problems. 
My only complaint is that there doesn't seem to be a way to suppress this 
compiler error:

com/google/common/primitives/UnsignedShorts.java:[24,15] Unsafe is internal 
proprietary API and may be removed in a future release

Original comment by cow...@bbs.darktech.org on 9 Sep 2011 at 2:16

GoogleCodeExporter commented 9 years ago
We're providing support for unsigned ints and longs, but shorts...I'm not sure 
they're used enough to be worth it.

Original comment by wasserman.louis on 29 Sep 2011 at 10:08

GoogleCodeExporter commented 9 years ago
Louis,

Our company uses CRC-16 for embedded devices we develop. If you Google "CRC-16" 
you will find that it brings up marginally less hits than "CRC-32" so I think 
it's fair to say that it's a fairly common use-case (at least in the embedded 
world). Besides, if you're already adding unsigned byte, int, and long support 
you might as well add unsigned short support for completeness sake. And there's 
also the fact that I've already provided a patch ;)

Original comment by cow...@bbs.darktech.org on 30 Sep 2011 at 3:51

GoogleCodeExporter commented 9 years ago
"Completeness' sake" is...a dangerous phrase.  (There have been things that I 
personally added to Guava for "completeness' sake," and now consider mistakes.  
I don't trust it.)

That said, I think Kevin supports this, which is good enough for me.

I wrote my own patch (I totally forgot yours was there, I'm afraid) and sent it 
for Kevin's review (at http://codereview.appspot.com/5271042/), which is 
consistent with UnsignedInteger, etc.  (In particular, it respects the wrapper 
type/view primitives as unsigned distinction.)

Original comment by wasserman.louis on 17 Oct 2011 at 5:30

GoogleCodeExporter commented 9 years ago
Issue 717 has been merged into this issue.

Original comment by wasserman.louis on 20 Oct 2011 at 8:36

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 10 Dec 2011 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 16 Feb 2012 at 7:17

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 30 May 2012 at 7:43

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 22 Jun 2012 at 6:16

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:18

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09