Signed bytes and comparaison.

In some part of the code, when i run findbugs, there is the following error reported :

"Bad comparison of signed byte : Signed bytes can only have a value in the range -128 to 127. Comparing a signed byte with a value outside that range is vacuous and likely to be incorrect. To convert a signed byte b to an unsigned value in the range 0..255, use 0xff & b".

It occurs for example with the following line :

byte[] data
if ((nonfullBitmap == -1) && (data[i] != 0xFF))

Is it something we must take care about or not ?


Fabien L.

Should be false

Imho we should care about these cases.
The code above evaluates to false as data[i] gets upcasted to int (range -128 to 127) so it'll never be 0xFF (Though I'm not sure about the intended semantics in that case).

Peter is absolutely correct

Any case of this reported by findbugs is deeply suspicious, and probably a real bug. But the only way to tell for sure ... and the only way to be sure of the correction ... is to examine the code carefully.