A Boolean operation combines or transforms truth-valued inputs according to a logical rule. In Java, related bitwise operations apply AND, OR, or XOR to individual bits, while precedence determines how compound expressions are grouped. section 2.1.2 section 2.2
Avoid these common Boolean-operation mistakes:
- Do not mix arithmetic and bitwise operators without parentheses: addition binds more tightly than bitwise AND, so bits & 0xFF00 + 1 is read as bits & (0xFF00 + 1). section 2.1.2
- Parenthesize bitwise combinations in conditions: comparison operators bind more tightly than bitwise OR, so flags == (Flags.ACC_PUBLIC | Flags.ACC_FINAL) expresses the intended test. section 2.2
- Treat bit manipulation as boundary-sensitive: shift counts wrap modulo 32 for int and modulo 64 for long, and the left operand determines the shift result type. section 4.12
Make the grouping explicit
The ambiguous expression `return bits & 0xFF00 | 1;` is clearer as `return (bits & 0xFF00) | 1;`. Parentheses show that the mask is applied before the low bit is set. section 2.1.2
int updateBits(int bits) {
return bits & 0xFF00 + 1;
}
Inspect the minimal expression whose result changes when arithmetic and bitwise precedence is misunderstood. section 2.1.2



