Set Clear Toggle a Specific Bit in DSA Java - Time & Space Complexity
We want to understand how long it takes to change a specific bit in a number.
How does the time needed grow when we work with bigger numbers?
Analyze the time complexity of the following code snippet.
public class BitOperations {
public static int setBit(int num, int pos) {
return num | (1 << pos);
}
public static int clearBit(int num, int pos) {
return num & ~(1 << pos);
}
public static int toggleBit(int num, int pos) {
return num ^ (1 << pos);
}
}
This code sets, clears, or toggles a bit at a given position in an integer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Bitwise shift and bitwise logical operations.
- How many times: Each operation runs once per method call, no loops or recursion.
The time to set, clear, or toggle a bit does not increase with the size of the number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (bits) | 1 operation |
| 100 (bits) | 1 operation |
| 1000 (bits) | 1 operation |
Pattern observation: The operation count stays the same no matter how big the number is.
Time Complexity: O(1)
This means the time to set, clear, or toggle a bit is constant and does not depend on input size.
[X] Wrong: "Changing a bit takes longer for bigger numbers because they have more bits."
[OK] Correct: Bitwise operations work directly on fixed-size integers, so changing one bit is always a single step.
Knowing that bit operations run in constant time helps you explain efficient low-level data manipulation clearly and confidently.
"What if we tried to set or clear bits in a very large number represented as an array of bits? How would the time complexity change?"
Practice
num | (1 << k) do to the integer num in terms of its bits?Solution
Step 1: Understand the bit shift operation
1 << kcreates a number with only the k-th bit set to 1 and all others 0.Step 2: Apply bitwise OR with
Usingnumnum | (1 << k)sets the k-th bit ofnumto 1 without changing other bits.Final Answer:
Sets the k-th bit of num to 1 -> Option CQuick Check:
Set bit = OR with 1 shifted [OK]
- Confusing OR with AND operation
- Thinking it clears or toggles the bit
- Misunderstanding bit positions starting at 0
num?Solution
Step 1: Understand clearing a bit
To clear a bit, we AND with a mask that has 0 at that bit and 1s elsewhere.Step 2: Create mask and apply
~(1 << 3)creates a mask with all 1s except 0 at bit 3. ANDingnumwith this clears bit 3.Final Answer:
num = num & ~(1 << 3); -> Option BQuick Check:
Clear bit = AND with NOT mask [OK]
- Using OR instead of AND to clear bits
- Using XOR which toggles bits
- Using AND with shifted 1 which keeps only that bit
int num = 10; // binary 1010 int k = 1; num = num ^ (1 << k); System.out.println(num);
Solution
Step 1: Understand initial value and bit positions
num = 10 in binary is 1010. Bit positions (0-based) from right: bit0=0, bit1=1, bit2=0, bit3=1.Step 2: Apply XOR to toggle bit 1
1 << 1is 0010. XOR toggles bit 1: 1010 ^ 0010 = 1000 (binary) which is 8 decimal.Final Answer:
8 -> Option DQuick Check:
XOR toggles bit 1: 10 -> 8 [OK]
- Confusing XOR with OR or AND
- Misreading bit positions starting at 1 instead of 0
- Incorrect binary to decimal conversion
num. What is the error?int num = 5; // binary 0101 int k = 2; num = num & (1 << k); System.out.println(num);
Solution
Step 1: Analyze the operation used
The code uses AND with1 << k, which keeps only the k-th bit and clears others.Step 2: Understand toggle operation
Toggle requires XOR, not AND. AND here does not toggle but masks all bits except bit 2.Final Answer:
It clears all bits except the 2nd bit instead of toggling -> Option AQuick Check:
Toggle = XOR, not AND mask [OK]
- Using AND instead of XOR to toggle bits
- Confusing bit positions
- Expecting output without understanding bitwise operations
num, write a Java expression to toggle the 0th bit only if it is currently set (1), otherwise leave num unchanged. Which expression achieves this?Solution
Step 1: Check if 0th bit is set
Use(num & (1 << 0)) != 0to test if bit 0 is 1.Step 2: Toggle bit 0 only if set
If bit 0 is set, toggle it with XOR; else keepnumunchanged.Final Answer:
num = (num & (1 << 0)) != 0 ? num ^ (1 << 0) : num; -> Option AQuick Check:
Conditional toggle only if bit set [OK]
- Toggling bit unconditionally
- Using OR which always sets bit
- Clearing bit without condition
Ready to prove it?
Pass the test and this pattern moves unknown
