Name: dm26566 Date: 11/03/98
Please extend the Java design to allow unsigned types, particularly
unsigned byte.
I have been wondering why there are no unsigned integer types in Java. It
seems to me that for byte-length values it is extremely awkward not to have
them, for short's it is somewhat awkward, and for 32-bit int's it is simply
awkward.
Let's focus on bytes for a minute. What good are signed bytes? Let's see,
maybe they could be used to record temperatures in Butte, Montana (Fahrenheit
or Centigrade!). But what are bytes most often used for? Let's see,
unsigned 8-bit pixels, unsigned characters that you are storing in an ascii
document, and unsigned values from 8-bit hardware registers. I don't mean to
put Butte down, but it's really no contest. If you _had_ to choose only
signed or unsigned, the statistically obvious choice would be unsigned.
A similar but less compelling case could be made for short's.
I recognize that this was a design decision made by the Java developers.
What I don't understand is why. Did they consider unsigned integer types evil
or harmful, and chose to protect me from myself? (That would be kind of like
Sears choosing not to sell hammers with its tool sets because they know that
some people (me especially!) would bang their thumbs.) Or is there actually
some good reason that I don't understand?
What I do know is that unsigned bytes and shorts have proven extremely
useful in my work with imagery (C language). Yes, I know I can process my
images so that I cast the bytes to shorts and and with 0xff -- but when you
have tens or hundreds of millions of pixels this is a rather absurd work
around. I don't know of a cpu that can't handle bytes naturally as unsigned
8-bit quantities (or maybe I should say it is no more work at the assembly
language level for the cpu to use unsigned bytes or signed bytes). I.e. the
choice in many cases is either (pseudo-assembler):
GetUnsignedByte:
clear reg1 ; 32-bit register
ldByte reg1, (fred) ; put the byte at address fred into reg1
or:
GetSignedByte:
ldByte reg1, (fred) ; put the byte at address fred into reg1
sxtByte reg1 ; echo the 7th bit thru bit 31
At this level, neither signed nor unsigned has a clear advantage or
disadvantage. So why can't Java offer us both?
(Review ID: 41171)
======================================================================