Scrivio Interview library
LeadershipIntermediate6 min read · updated 12 November 2026

In code review you see if (a & MASK == 0) and return flags << 1 + shift;. What is wrong, and what rule would you set for the team?

In a & MASK == 0, the == binds more tightly than &, so it means a & (MASK == 0): for an int a it does not compile, because int & boolean is not allowed, and for a boolean a it compiles and silently tests something else. In flags << 1 + shift, + binds more tightly than <<, so it shifts by 1 + shift instead of shifting and then adding. The team rule I would set is to parenthesise every expression that mixes bitwise or shift operators with arithmetic or comparison operators, and to enable static analysis checks for suspicious precedence, because formatters do not add parentheses.

Why this question is asked

Bitwise code is rare in most business applications, which is exactly why its precedence rules catch experienced engineers: nobody remembers that comparison binds tighter than bitwise and, or that addition binds tighter than shift. These bugs are especially dangerous when they compile, because the code looks right to the reviewer too. Interviewers ask this to see whether we can spot the problem and then turn one review comment into a rule and a tool that prevent the whole class of bugs. In this answer, we will recap where these operators sit in Java's precedence table, analyse both lines, see why the shift case is the more dangerous one, and set a rule with enforcement.

1A Quick Recap of Bitwise, Shift and Comparison Precedence

A mask is an int whose bits select a subset of flags, such as MASK = 0b0100 for the third bit. a & MASK keeps only that bit of a, so (a & MASK) == 0 asks whether the bit is clear. A shift moves bits: flags << 1 shifts every bit one place to the left, which multiplies by 2.

Java inherited its precedence order from C, where the bitwise operators were placed below comparison for historical reasons. The result is an order most people do not expect.

Relevant precedence levels, highest first
LevelOperatorsSurprise
multiplicative* / %
additive+ -binds tighter than shifts
shift<< >> >>>
relational< > <= >= instanceof
equality== !=binds tighter than & ^ |
bitwise and&
bitwise xor^
bitwise or|
logical and / or&& ||
Both lines in the question break on one of the two marked surprises.

2Line 1: a & MASK == 0

Because == has higher precedence than &, Java groups the expression as a & (MASK == 0). MASK == 0 is a boolean. What happens next depends on the type of a.

Java
static final int MASK = 0b0100;
int a = 0b0011;
if (a & MASK == 0) { } // error: bad operand types for binary operator '&'
if ((a & MASK) == 0) { } // compiles: true, because bit 2 of a is clear
boolean b = true;
if (b & MASK == 0) { } // compiles: b & false, which is false
// the author meant to test a bit; the code tests whether MASK is zero

For an int, the compiler saves us: int & boolean is not a legal combination, and javac reports bad operand types. That is the lucky case: in C, where comparisons produce ints, the same line would compile and quietly do the wrong thing. But if a is a boolean, & is the non-short-circuit logical and, and b & (MASK == 0) compiles. It no longer tests a bit at all, it asks whether a constant is zero, which is always false here, so the branch is silently dead.

3Line 2: flags << 1 + shift

Additive operators bind more tightly than shifts, so flags << 1 + shift means flags << (1 + shift). It always compiles, because both readings are int expressions, and it gives a different number.

Java
int flags = 1, shift = 2;
System.out.println(flags << 1 + shift); // 8: 1 << (1 + 2)
System.out.println((flags << 1) + shift); // 4: (1 << 1) + 2
int big = 1 << 32 + 1; // 1 << 33, and int shifts use only the low 5 bits: 1 << 1
System.out.println(big); // 2
Step throughHow the reviewer should read the expression
  1. Find the operators<< and +
  2. Look up their precedence+ is higher than <<
  3. Group the expressionflags << (1 + shift)
  4. Matches the author's intent?
    YesAdd parentheses anyway, to make it obviousNoBug: rewrite as (flags << 1) + shift

4 steps in order. Play them, or step through them one at a time.

Either way, the expression ends with explicit parentheses.

Notice that the wrong answer is not obviously wrong: 8 and 4 are both plausible flag values. This is the more dangerous of the two lines, because there is no compile error, and a test with shift = 0 gives the same result either way, 2, so even a test may not catch it. The last example adds one more trap: shift distances for int are taken modulo 32, so shifting by 33 is shifting by 1.

4The Team Rule: Parenthesise Mixed Operator Families

One review comment fixes one line; a rule fixes the habit. The rule I would set is short: whenever an expression mixes bitwise or shift operators with arithmetic, comparison or logical operators, parenthesise every group, even where the default precedence happens to give the intended result. So we write (a & MASK) == 0, (flags << 1) + shift, and (x & 0xFF) | (y << 8).

The reason to require parentheses even when they are redundant is that the reader cannot tell otherwise whether the author knew the rule or got lucky. Parentheses cost nothing at runtime and make the intent checkable. For flag-heavy code, the rule also encourages small named helpers such as isSet(flags, MASK), or using EnumSet instead of int masks where performance allows, which removes the bit operations from business code entirely.

5Enforce It With Tools, Not Memory

Formatters such as google-java-format and the IDE's reformat change whitespace and line breaks; they never add parentheses, because that would change the code's structure. So formatting does not help. Static analysis does: Error Prone has an OperatorPrecedence check that warns on expressions mixing such operators without parentheses, IntelliJ IDEA has inspections for unclear expressions, and Sonar and SpotBugs have rules for suspicious operator use, such as comparing the result of a bitwise operation in surprising ways. Check your tool versions for the exact rule names.

We enable these checks in the build for the whole team, as warnings first and errors once existing code is cleaned up, so the next mixed expression is flagged before a reviewer ever sees it. And we add the two lines from this review as unit tests for the code they came from, because a bug that reached review once deserves a test that pins its fix.

6Follow-up questions

Why does the compiler accept b & MASK == 0 without a warning?

Because it is legal: & is defined for two booleans as a logical and that evaluates both sides, and MASK == 0 is a boolean. The compiler checks types, not intent. Static analysis exists for exactly this gap.

Would you ask for parentheses even in a + b * c?

No. Arithmetic precedence is taught in school and every reader knows it. The rule targets operator families whose relative precedence people genuinely misremember: bitwise, shift, comparison and the mix of && with ||.

How do you raise this in review without sounding pedantic?

By showing the evaluated result rather than citing the rule: this expression is 1 << 3, which gives 8, not 4, so did you mean (flags << 1) + shift? A concrete value turns a style comment into a correctness question, and linking to the team rule makes it clear it is not personal preference.

7Conclusion

In this answer, we saw that == binds more tightly than &, so a & MASK == 0 means a & (MASK == 0), which fails to compile for an int and silently tests the wrong thing for a boolean, and that + binds more tightly than <<, so flags << 1 + shift shifts by 1 + shift. The team rule is to parenthesise every expression that mixes bitwise or shift operators with arithmetic, comparison or logical ones, enforced by static analysis such as Error Prone's precedence check, because formatters never add parentheses.

What to say in the interviewAbout 100 seconds aloud

Open with

Both lines are precedence bugs inherited from C's operator table. In the first, equality binds tighter than bitwise and, so a & MASK == 0 is read as a & (MASK == 0).

Then, if they want more

If a is an int, that does not compile, because you cannot and an int with a boolean, which is the lucky case. If a is a boolean, it compiles as a non-short-circuit logical and with MASK == 0, so it silently stops testing the bit and just checks whether the constant is zero.

The intended code is (a & MASK) == 0. In the second, addition binds tighter than shift, so flags << 1 + shift means flags << (1 + shift). It always compiles and returns a plausible number, for example 8 instead of 4, and with shift equal to zero the two readings agree, so tests may not catch it.

That makes it the more dangerous one. For the team, I would set a simple rule: whenever an expression mixes bitwise or shift operators with arithmetic, comparison or logical operators, parenthesise every group, even if the default precedence happens to be right, because the reader cannot tell whether the author knew the rule.

I would enforce it with tools rather than memory, for example Error Prone's OperatorPrecedence check or the equivalent IDE and Sonar inspections, because formatters never add parentheses. And in review I would show the evaluated value, which makes it a correctness discussion rather than a style one.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In code review you see if (a & MASK == 0) and return flags << 1 + shift;. What is wrong...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/352a7fc7b7/