Sunday, 29 September 2013

Can the result of a bitwise AND operator be negative (in Java)

Can the result of a bitwise AND operator be negative (in Java)

I have the following piece of code:
int SOME_MASK = 0x0000ffff;
int value = /* some value */;
int something = SOME_MASK & value;
// WHY IS "something" guaranteed to be non-negative ?
if (something != NEGATIVE_CONSTANT) {
// do something here....
}
I keep getting the FindBugs analysis warning:
Correctness - Bad comparison of nonnegative value with negative constant
This code compares a value that is guaranteed to be non-negative with a
negative constant.
The warning pops up for the line where comparing the bitwise AND result
with the negative constant.
I am not sure why a Bitwise AND result is guaranteed to be non-negative?
is this always the case?

No comments:

Post a Comment