What Are the Core Bit Manipulation Operators and When Do You Use Them?
Learn the core bitwise operators, set/clear/test/toggle bit idioms, and how to answer this bit manipulation interview question.
Expected Interview Answer
Bit manipulation uses the six operators AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>) to inspect, set, clear, or toggle individual bits directly, giving O(1) operations that are far cheaper than arithmetic equivalents for tasks like flags, masks, and fast multiply/divide by powers of two.
AND is used to test or clear specific bits by masking against a pattern; OR sets bits on without disturbing others; XOR toggles bits and is the basis of swap and parity tricks; NOT flips every bit, useful for building inverse masks. Left shift n << k multiplies n by 2^k and right shift n >> k divides by 2^k (for non-negative integers), both in constant time versus a loop-based multiply. Together these let you pack multiple boolean flags into a single integer, check if a number is even (n & 1), or check power-of-two (n & (n-1) == 0). The tradeoff is readability โ bit tricks are fast but need comments, since the logic is not self-evident like a named boolean check.
- O(1) per-bit operations, no loops needed
- Compact storage of many flags in one integer
- Faster than arithmetic for power-of-two multiply/divide
- Foundation for XOR swap, masking, and parity tricks
AI Mentor Explanation
Think of a scorecard using one physical light switch per fielding position to mark whether that fielder is currently on the field: flipping a switch on is like OR-ing a bit to 1, flipping it off is like AND-ing with a mask of 0 at that position. Checking whether the wicketkeeper switch is on without touching any other switch is exactly an AND with a mask that isolates just that bit. XOR is like a switch that always flips state when pressed, useful for toggling a "rain delay" indicator on and off with the same button. NOT flips every switch on the board at once, giving you the opposite lineup instantly rather than checking each fielder one by one.
Step-by-Step Explanation
Step 1
Set a bit
n | (1 << k) turns on bit k without disturbing any other bit.
Step 2
Clear a bit
n & ~(1 << k) turns off bit k, leaving the rest unchanged.
Step 3
Test a bit
(n >> k) & 1, or n & (1 << k) != 0, checks whether bit k is set.
Step 4
Toggle a bit
n ^ (1 << k) flips bit k regardless of its current value.
What Interviewer Expects
- Name all six operators and what each does at the bit level
- Show the set/clear/test/toggle bit-mask idioms from memory
- Explain shift-as-multiply/divide and its power-of-two limitation
- Give a real use case: flags, parity, or power-of-two checks
Common Mistakes
- Confusing logical AND/OR (&&, ||) with bitwise AND/OR (&, |)
- Forgetting NOT (~) flips ALL bits, including sign bit on signed integers
- Using shifts for multiply/divide on non-powers of two and expecting correctness
- Not masking after a shift, letting unwanted high bits leak into a result
Best Answer (HR Friendly)
โBit manipulation means working directly with the 0s and 1s inside a number using operators like AND, OR, XOR, and shifts. I use it to pack several true/false flags into one compact number, or to write very fast checks like "is this number even" or "is this a power of two," which run in constant time.โ
Code Example
def set_bit(n, k):
return n | (1 << k)
def clear_bit(n, k):
return n & ~(1 << k)
def test_bit(n, k):
return (n >> k) & 1 == 1
def toggle_bit(n, k):
return n ^ (1 << k)
def is_power_of_two(n):
return n > 0 and (n & (n - 1)) == 0
print(set_bit(0b0010, 0)) # 0b0011
print(clear_bit(0b0011, 1)) # 0b0001
print(test_bit(0b0101, 2)) # True
print(is_power_of_two(16)) # TrueFollow-up Questions
- How would you count the number of set bits in an integer efficiently?
- How does a bitmask represent a subset in subset-enumeration problems?
- Why does n & (n - 1) clear the lowest set bit, and where is that useful?
- How would you check if two integers differ in exactly one bit?
MCQ Practice
1. Which expression clears bit k of integer n?
ANDing with the complement of a mask that has only bit k set turns off bit k while leaving all other bits unchanged.
2. What does n << 3 compute for a non-negative integer n?
A left shift by k positions multiplies n by 2^k, so shifting left by 3 multiplies n by 8.
3. Which operator toggles bit k regardless of its current value?
XOR with a mask that has only bit k set flips that bit: 0 becomes 1 and 1 becomes 0, leaving other bits untouched.
Flash Cards
How do you set bit k of an integer n? โ n | (1 << k)
How do you test whether bit k of n is set? โ (n >> k) & 1 == 1
What does a left shift by k do to a non-negative integer? โ Multiplies it by 2^k in constant time.
Which operator flips a bit on if off and off if on? โ XOR (^), used for the toggle-bit idiom.