WORK AROUND
Name: dm26566 Date: 11/03/98
int biggerThanIneed = (int)(my8bitByte) & 0xff;
======================================================================
|
|
|
PUBLIC COMMENTS
he omission of unsigned integral types from Java is somewhat controversial,
but they are in fact inessential, if perhaps convenient nonetheless. They
are most often desired in "bit-bashing" code, such as the examples you cite.
Note that in two's complement arithmetic, addition and subtraction are performed
identically on both signed and unsigned quantities. That is, the signed/unsigned distinction is simply a matter of how we interpret the operands
and the result, not what the operation actually does. Java provides several
mechanisms to make it easy to exploit this equivalence when writing bit-bashing
code: 1) Octal and hexadecimal literals specify a bit-pattern, not a positive
magnitude, i.e., bit-patterns representing negative numbers may be written
just the same as a bitwise-identical unsigned value would be written, and 2) both logical and arithmetic shift operations are provided. The logical shift
also serves as an unsigned multiplication by a power of two, just as an
arithmetic shift performs that operation for signed integers. These facilities
are entirely adequate for manipulating integers as small bit-vectors, but do
fall short in some cases where we are actually interested in numerical magnitudes.
|
|
|
EVALUATION
The omission of unsigned integral types from Java is somewhat controversial,
but they are in fact inessential, if perhaps convenient nonetheless. They
are most often desired in "bit-bashing" code, such as the examples you cite.
Note that in two's complement arithmetic, addition and subtraction are performed
identically on both signed and unsigned quantities. That is, the signed/unsigned distinction is simply a matter of how we interpret the operands
and the result, not what the operation actually does. Java provides several
mechanisms to make it easy to exploit this equivalence when writing bit-bashing
code: 1) Octal and hexadecimal literals specify a bit-pattern, not a positive
magnitude, i.e., bit-patterns representing negative numbers may be written
just the same as a bitwise-identical unsigned value would be written, and 2) both logical and arithmetic shift operations are provided. The logical shift
also serves as an unsigned multiplication by a power of two, just as an
arithmetic shift performs that operation for signed integers. These facilities
are entirely adequate for manipulating integers as small bit-vectors, but do
fall short in some cases where we are actually interested in numerical magnitudes.
william.maddox@Eng 1998-11-06
Thanks, Bill. Nicely said.
In other words, it doesn't matter very much at all, so we should not destabilize the language with trivialities.
gilad.bracha@eng 1998-11-06
|
|
|
|