Hi -
Are unsigned bytes difficult to use in Java? Or am I doing
something incredibly stupid?
My approach so far has been: promote everything to an
int either implicitly or explicitly, operate, cast the
result to a byte.
Really makes some ugly code. Seems error prone too. The
faq says something like "it's really hard to determine
signed/unsigned rules in an expression tree." Alright,
fair enough. But I can't help wondering how difficult
it is to implement it in the compiler versus burdening
the programmer with it over and over again.
I should probably crack open the compression package source
code and see how Sun did the zip stuff. I'm guessing there's
a lot of byte level stuff there. And I'm guessing it'll
be a myriad of casts.
Thanks,
Brian
Real Gagnon - 20 Sep 2005 22:07 GMT
> Are unsigned bytes difficult to use in Java? Or am I doing
> something incredibly stupid?
>
> My approach so far has been: promote everything to an
> int either implicitly or explicitly, operate, cast the
> result to a byte.
public static int unsignedByteToInt(byte b) {
return (int) b & 0xFF;
}
more at http://www.rgagnon.com/javadetails/java-0026.html
Bye.

Signature
Real Gagnon from Quebec, Canada
* Looking for Java or PB code examples ? Visit Real's How-to
* http://www.rgagnon.com/howto.html
Oliver Wong - 20 Sep 2005 22:16 GMT
>> Are unsigned bytes difficult to use in Java? Or am I doing
>> something incredibly stupid?
[quoted text clipped - 8 lines]
>
> more at http://www.rgagnon.com/javadetails/java-0026.html
No, I think unsigned byte arithmatic is difficult to do in Java. Simply
promoting them to ints doesn't nescessarily solve the problem, if your
algorithm depends on bit shifting.
You could actually create an "Unsigned Byte" class, and define all the
operations, but it'd probably be slow. Best to just rethink the algorithms
from scratch in those cases.
- Oliver
Roedy Green - 21 Sep 2005 00:41 GMT
>Are unsigned bytes difficult to use in Java? Or am I doing
>something incredibly stupid?
see http://mindprod.com/jgloss/unsigned.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Roedy Green - 21 Sep 2005 00:42 GMT
>Really makes some ugly code. Seems error prone too.
Since bytes get cast to ints on almost any operation, you have to keep
doing & 0xff after a load to keep them unsigned.
They truncate with just a cast on store.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Dale King - 22 Sep 2005 02:20 GMT
> Really makes some ugly code. Seems error prone too. The
> faq says something like "it's really hard to determine
> signed/unsigned rules in an expression tree." Alright,
> fair enough. But I can't help wondering how difficult
> it is to implement it in the compiler versus burdening
> the programmer with it over and over again.
That logic does apply to unsigned int. C has complicated rules to handle
operations involving signed and unsigned integers that are prone to errors.
That logic does *not* apply for unsigned byte. There is no reason that
they shouldn't have had an unsigned byte. The rules are very simple.
Java promotes everything smaller than an int to int. All you have to do
is not sign extend an unsigned byte.
So, I agree the lack of an unsigned byte type was a major oversight in Java.

Signature
Dale King
Roedy Green - 22 Sep 2005 04:58 GMT
>That logic does *not* apply for unsigned byte. There is no reason that
>they shouldn't have had an unsigned byte. The rules are very simple.
>Java promotes everything smaller than an int to int. All you have to do
>is not sign extend an unsigned byte.
The load-byte JVM instruction does a sign extend. So to add an
unsigned byte, you would need a new op code, or you would have to add
AND 0xff after every load, that hopefully any JIT or AOT would
optimise away.
You also need to modify the class file format to allow unsigned byte
as a type in method signatures.
It was a mistake to make byte signed in the first place. Every time I
have wanted a byte, I wanted an unsigned one. Certainly compared with
some of the other changes Sun made, this would be trivial.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.