Interview Q&A · Java · 4 questions in this topic

Bitwise & Shift Operators — Java Interview Q&A

All Java topics

Bit-level operators, and the frequently tested difference between signed and unsigned right shift. Part of Core Java Basics · 294 questions in the Java bank.

Bitwise operators work on the two’s-complement representation of integers, which is why shift behaviour depends on the operator rather than the value. Use >> for arithmetic division by powers of two on signed values, and >>> when the value is a bit pattern or hash rather than a number. Shifts mask the distance to 5 bits for int and 6 for long, so x << 32 is x << 0; write masks and flags with & and |, and test bits with & rather than the short-circuiting &&.

What is the difference between >> and >>> in Java?

Answer

Both shift bits to the right, but >> is the signed (arithmetic) shift: it preserves the sign by filling vacated high bits with the original sign bit, so negative numbers stay negative. >>> is the unsigned (logical) shift: it always fills with zero, so it treats the value as an unsigned 32-bit (int) or 64-bit (long) number and the result is never negative for ints.

Example

Example.javajava
int a = -8;        // ...11111000 in two’s complement
a >> 2;            // -2  (sign bit replicated)
a >>> 2;           // 1073741822 (zeros shifted in)

What is the difference between & and && in Java?

Answer

Both mean “and”, but && short-circuits: if the left operand is false, the right operand is never evaluated. & always evaluates both sides. For booleans that difference is observable when the right side has a side effect or can throw, such as a null check followed by a dereference. & is also the bitwise AND for integer types, where short-circuiting has no meaning. Use && for conditions and & only when you deliberately want both sides evaluated or are masking bits.

Key points

  1. 1&& and || short-circuit; & and | do not.
  2. 2& on integers performs a bitwise AND.
  3. 3A null guard followed by a dereference must use &&.
  4. 4Bit masking should use &, never &&.

What are the typing rules for the ternary operator?

Answer

condition ? a : b evaluates one of the two branches and has a type determined from both. If one branch is a primitive and the other is a boxed type, the result is unboxed, which can throw a NullPointerException when the boxed side is null. If the branches are unrelated reference types the result is their least common supertype, often Object. A ternary is an expression, so it cannot be used as a statement on its own, and both branches must be assignment-compatible with the target.

Key points

  1. 1Mixed primitive and boxed branches cause unboxing.
  2. 2Unrelated reference branches widen to a common supertype.
  3. 3A ternary is an expression, never a statement.
  4. 4Nested ternaries are legal but rarely readable.

Example

Example.javajava
Integer boxed = null;
int x = true ? 1 : boxed;   // NPE — the ternary unboxes
Object o = true ? "s" : 1;  // String and Integer meet at Object

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits