Interview Q&A · Java · 4 questions in this topic
Bitwise & Shift Operators — Java Interview Q&A
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
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&& and || short-circuit; & and | do not.
- 2& on integers performs a bitwise AND.
- 3A null guard followed by a dereference must use &&.
- 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
- 1Mixed primitive and boxed branches cause unboxing.
- 2Unrelated reference branches widen to a common supertype.
- 3A ternary is an expression, never a statement.
- 4Nested ternaries are legal but rarely readable.
Example
Integer boxed = null; int x = true ? 1 : boxed; // NPE — the ternary unboxes Object o = true ? "s" : 1; // String and Integer meet at Object
Why is 0.1 + 0.2 not equal to 0.3?
Answer
float and double use IEEE 754 binary floating point, which cannot represent 0.1, 0.2 or 0.3 exactly — just as decimal cannot represent one third. Each literal is stored as the nearest binary fraction, and adding the two approximations produces a value a few ulps away from the approximation of 0.3, so == returns false. Use BigDecimal for money and other exact decimal arithmetic, or compare doubles with a tolerance when the quantity is genuinely continuous.
Key points
- 1Binary floating point cannot represent most decimal fractions exactly.
- 2Never compare floating-point results with ==.
- 3BigDecimal with a scale handles money correctly.
- 4For measurements, compare the difference against an epsilon.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission