DSA / 6 min read
Day 11 of 60 Days of DSA: Bit Manipulation
Why “Simple” Bit Questions Eliminate More Candidates Than Hard DSA Problems
Day 11 of 60 Days of DSA: Bit Manipulation
Why “Simple” Bit Questions Eliminate More Candidates Than Hard DSA Problems

Bit manipulation looks easy — until an interview turns it into a deal-breaker. Many candidates skip it (I was one of them), assuming it’s “basic,” only to realise too late until I faces bit manipulation questions in interviews.
These questions aren’t about memorising operators. They reveal whether you truly understand how numbers exist inside a system, how bits shift, flip, and interact, and how well you can reason at a low level under pressure.
Before we dive in, it’s worth revisiting the fundamentals of bits and bitwise operators. A strong grasp here will not only make these problems easier — but will also help you think faster and more confidently during interviews.
We’re going to see 3 sets of problems today
- Number of 1 Bits
- Set Bit
- Unset ith Bit
Problem 1: Number of 1 Bits
Problem Description
Write a function that takes an integer and returns the number of 1 bits present in its binary representation.
Example Input
Input 1: 11
Input 2: 6
Example Output
Output 1: 3
Output 2: 2
Example Explanation
Explanation 1: 11 is represented as 1011 in binary.
Explanation 2: 6 is represented as 110 in binary.
Intuition
If at each bit of the number we can check whether the bit is set ot not and increment the count on basis of that
Code (In JavaScript)
function numberOf1Bits(A){
let count = 0;
while(A){
if((A & 1) ==1) count++;
A = A>>1
}
return count;
}Code Explanation
- A&1 will do the AND of the last bit of A with 1
- If the last bit is 1, then the condition will become true and the count will be incremented, i.e, the bit is set
- A = A >> 1, This will right shift the A by 1 bit or we can say divide the A/2 so that we can check the next bit until A becomes 0, which will give us the number of set bits in A
Problem 2: Set Bit
Problem Description
You are given two integers A and B.
Set the A-th bit and B-th bit in 0, and return output in decimal Number System.
Note: The bit positions are 0-indexed, which means that the least significant bit (LSB) has index 0.
Example Input
Input 1:
A = 3
B = 5
Input 2:
A = 4
B = 4
Example Output
Output 1: 40
Output 2: 16
Example Explanation
For Input 1: The binary expression is 101000, which is 40 in decimal.
For Input 2: The binary expression is 10000, which is 16 in decimal
Intuition
The intuition behind this code is to build a number from scratch by selectively turning ON specific bits, while keeping all other bits unchanged.
The variable n starts as 0, which in binary means every bit is OFF. This gives a clean base where no bits are set initially.
When we do 1 << A, we create a number where only the A-th bit is ON and all other bits are OFF. Using the OR (|) operator with n ensures that this bit gets turned ON. OR is used because:
0 | 1 = 1 → the bit becomes set
1 | 1 = 1 → the bit stays set
0 | 0 = 0 → no unwanted changes
So after n = n | (1 << A), the A-th bit is guaranteed to be ON.
The same logic is applied again with 1 << B. OR-ing this mask with n turns ON the B-th bit without affecting the A-th bit or any other bit that may already be set.
Code (In JavaScript)
function setBit(A, B){
let n =0;
n = n | 1<<A;
n = n | 1<<B;
return n;
}Problem 3: Unset ith Bit
Problem Description
You are given two integers A and B.
If B-th bit in A is set, make it unset.
If B-th bit in A is unset, leave as it is.
Return the updated A value.
Note: The bit position is 0-indexed, which means that the least significant bit (LSB) has index 0.
Example Input
Input 1:
A = 4
B = 1
Input 2:
A = 5
B = 2
Example Output
Output 1: 4
Output 2: 1
Example Explanation
For Input 1:
Given N = 4 which is 100 in binary. The 1-st bit is already unset
For Input 2:
Given N = 5 which is 101 in binary. We unset the 2-nd bit
It becomes 001 which is 1 in Decimal.
Code (In JavaScript)
function unsetIthBit(A, B){
if(A & 1<<B){
A = A ^ 1<<B;
}
return A;
}Explanation
To unset the i-th bit, the idea is to target only that specific bit while leaving all other bits untouched.
We first create a number where only the i-th bit is set by left-shifting 1 (1 << i). This gives us a mask that points exactly to the bit we care about.
Before changing anything, we check whether the i-th bit in the original number is already set. Using AND (&) helps here because:
1 & 1 = 1 → the bit is set
1 & 0 = 0 → the bit is not set
If the result is non-zero, we know the bit is currently 1.
Now comes the role of XOR (^). XOR is useful because it flips bits:
1 ^ 1 = 0 → a set bit becomes unset
0 ^ 1 = 1 → an unset bit becomes set
Since the mask has 1 only at the i-th position, XOR affects only that bit and nothing else. When the bit is already set, XOR with the mask turns it into 0, effectively unsetting it. All other bits remain unchanged because XOR with 0 does nothing.
That’s why XOR works well here: it precisely toggles the target bit without disturbing the rest of the number.
Complexities
Time Complexity — O(1) (for each solution, as for a number, only 32 bits are present, so even if we’re using a loop still it’ll still be constant.)
Space Complexity — O(1) (No extra space used)
Do you want to solve more with us?
Then follow this series and share your progress in the comments.
If the topics are difficlt for you to understand, then you can refer to the notes on my GitHub.
Here’s the Code link
At Dev Simplified, We Value Your Feedback 📊
👉 Let us know how you’re finding this series so far. It motivates us to write and provide valuable content for you.
👉 Follow us to not miss any updates.