Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Return `(n & (1 << k)) != 0`, or equivalently `((n >> k) & 1) == 1`.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you build a mask correctly and test it without the == 1 bug.
Where candidates slip
`(n & mask) == 1` failing for higher bits.
How to stand out
Note set/clear/toggle all build on this mask idiom.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Mask and test
1Hint 1Explore hintHide hint
How do you build a number with only bit k set?
2Hint 2Explore hintHide hint
AND that mask with n to isolate bit k.
3Hint 3Explore hintHide hint
Why is comparing to 1 wrong for k > 0?
A · Core idea
Test whether bit k (0-indexed from the LSB) is 1 by building a single-bit mask 1 << k and ANDing it with n: a non-zero result means the bit was set.
Why `1 << k`. Shifting 1 left by k positions yields a number with a single 1 at position k (e.g. 1 << 2 = 100). ANDing n with this mask zeroes every other bit, isolating bit k.
Why compare `!= 0`, not `== 1`. For k > 0 the isolated bit's value is 2ᵏ, not 1 — so (n & mask) == 1 is wrong for any k > 0. Test != 0. The equivalent (n >> k) & 1 == 1 shifts the bit down to position 0 first, where == 1 is valid.
This masking idiom is the foundation for setting (n | mask), clearing (n & ~mask), and toggling (n ^ mask) bits.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 5 (101), k = 2mask
1 << 2 = 100
mask=4trueC · Code with teaching comments
D · Why it works and complexityO(1) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(1) time, O(1) space — one shift and one AND — constant time.
E · Edge cases and pitfalls5n = 5, k = 0 — bit 0 of 101 is 1 → true.Explore edge casesHide edge cases
Edge cases handled
n = 5, k = 0bit 0 of 101 is 1 → true.n = 5, k = 1bit 1 is 0 → false.n = 0, k = 3All bits 0 → false.
Pitfalls
- Comparing `(n & mask) == 1` instead of `!= 0` (fails for k > 0).
- Off-by-one in the bit position.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic boolean isBitSet(int n, int k) { return (n & (1 << k)) != 0; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Comparing the masked value to 1
For k>0 the isolated bit equals 2^k, not 1, so == 1 is false even when the bit is set. Compare != 0.
Explore the code comparisonHide the code comparison
Comparing the masked value to 1
For k>0 the isolated bit equals 2^k, not 1, so == 1 is false even when the bit is set. Compare != 0.
Don't write this
return (n & (1 << k)) == 1; // wrong for k>0Write this instead
return (n & (1 << k)) != 0;Revise
Carry the pattern into the interview
Pattern to remember
Shifted mask
Code pattern to memorize
(n & (1<<k)) != 0 or (n>>k)&1Rules to remember
- 1mask = 1 << k.
- 2n & mask != 0.
- 3Or (n>>k)&1.
Use this pattern when
- Reading a specific bit
- Bit flags / bitmasks
Interview takeaway
1<<k builds a single-bit mask to test a position.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Shifted mask. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups