views:

1364

answers:

7

Hi

how can i declare an unsigned short value in Java?

Thank you

+2  A: 

No such type in java

lewap
+7  A: 

You can't, really. Java doesn't have any unsigned data types, except char.

Admittedly you could use char - it's a 16-bit unsigned type - but that would be horrible, as char is clearly meant to be for text.

Jon Skeet
Another great answer. Jon Skeet, you are my hero.
Yada
+1  A: 

Java does not have unsigned types. What do you need it for?

Java does have the 'byte' data type, however.

CBFraser
byte is 8 bit, short is 16 bit... don't think byte will work :-)
TofuBeer
i would like to build a multi-dimensional array with for example 10000*10000 entries of short numbers... thats why i thought of unsigned shorts, for allocating less memory
maiky
Thanks for the correction, TofuBeer. Too quick on the draw, I guess.
CBFraser
Makes sense, @maiky. Also, if you're worried about memory, you could probably lift the tricks from sparse matrices if you anticipate a lot of entries in your array will be zero.
CBFraser
A: 

Yep no such thing if you want to use the value in code vs. bit operations.

Xepoch
+3  A: 

You can use a char, as it is an unsigned 16 bit value (though technically it is a unicode character so could potnetially change to be a 24 bit value in the future)... the other alternative is to use an int and make sure it is within range.

Don't use a char - use an int :-)

And here is a link discussing Java and the lack of unsigned.

TofuBeer
char is defined to be 16 bits, not a Unicode character (whatever that means), always and forever. If char changed to 24 bits, it would no longer be Java.
Ken
I don't think it'll ever change either. The reason why 16 bits is to support unicode (from the JLS: "The Java platform tracks the Unicode specification as it evolves." and "The Unicode standard was originally designed as a fixed-width 16-bit character encoding") and from java.lang.Character: "The methods that only accept a char value cannot support supplementary characters" - so origianlly char was 16 bit because that was how wide unicode was. Now unicode is larger and char can no longer represent all unicode characters.
TofuBeer
A: 

You can code yourself up a ShortUnsigned class and define methods for those operators you want. You won't be able to overload + and - and the others on them, nor have implicit type conversion with other primitive or numeric object types, alas.

Like some of the other answerers, I wonder why you have this pressing need for unsigned short that no other data type will fill.

Carl Smotricz
A: 

If you really need a value with exactly 16 bits:

Solution 1: Use the available signed short and stop worrying about the sign, unless you need to do comparison (<, <=, >, >=) or division (/, %, >>) operations. See this answer for how to handle signed numbers as if they were unsigned.

Solution 2 (where solution 1 doesn't apply): Use the lower 16 bits of int and remove the higher bits with & 0xffff where necessary.

starblue