unsigned : Java Glossary

go to home page U words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2009 Roedy Green, Canadian Mind Products
unsigned
in Java bytes, shorts, ints and longs are all considered signed. The only unsigned type is the 16-bit char. To use the sign bit as an additional data bit you have to promote to the next bigger data type with sign extension then mask off the high order bits. i.e. To get the effect of an 8-bit unsigned:
byte b;
// ...
int i = b & 0xff;
To get the effect of a 16-bit unsigned use char.

To get the effect of a 32-bit unsigned:

int i;
// ...
long l = i & 0xffffffffL;
If you have an extensive amount of unsigned work to do, especially 64-bit unsigned, you might find the WBEM classes useful.

Here is how to combine two unsigned shorts into an int.

// combining two unsigned shorts into an unsigned int.
short ush = 4;
short usl = 9999;
int ucombined = ( ush & 0xffff ) << 16 | ( usl & 0xffff );

For 64 bit unsigned, consider that addition and subtraction give you the same results whether you consider the operands signed or unsigned. When you multiply an two unsigned 64 bit operands together you get a 128 bit result which won’t fit in a long anyway, so 64 bit unsigned multiply is not useful. To implement an unsigned 64 bit division, you could handle it 32 bits as a time, much the way you handled decimal division in grade 4. Check the signs first, if they are 0 just use ordinary division.

You could store a signed or unsigned number in byte, char, int or long.

Sometimes you get the same result in terms of bits whether you treat quantities as signed or unsigned.

Does Signed/Unsigned Matter?
Operator 8 bit 16 bit 32 bit 64 bit
= load
= store
+- addition/subtraction
* multiplication
/ division
% remainder
== != equality
< <= > >= comparison
& | ~ ! bitwise
>>> >> << shift

For large unsigned numbers, look into BigInteger and a BigDecimal.

Displaying

Java’s Integer.toString interprets the value as signed. Here is how to treat it as unsigned.
/**
 * Convert an unsigned int to a String.
 * @param i the int to convert.
 * @return the equivalent unsigned String
 */
public static void unsignedToString( int i )
   {
   return Long.toString( i & 0xffffffffL )
   }
To display unsigned longs, use Long.toHexString. Writing a base 10 unsigned converter would be a challenge.

Gotchas

Working with a mixture of constants and bytes, it is easy to trip up when int constants don’t sign extend and (byte) constants do. Consider this example:

Why?

Gosling, one of the creators of Java, said he left out unsigned types because of the way they complicated the C and C++ languages. They create too many corner cases where gotchas hide.

I see so more problem than with char. Unsigned bytes could be promoted to int, just like char.

I think the reason was the original C had signed bytes. Back then, 7-bit ASCII was in vogue, which did not need the sign bit. The sign bit was free to use as a special flag.


CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/unsigned.html J:\mindprod\jgloss\unsigned.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.108]
You are visitor number 56,849.