What does it mean to set a specific bit in a number?
Setting a bit means changing that bit to 1, regardless of its current value.
Click to reveal answer
beginner
How do you clear a specific bit in a number?
Clearing a bit means changing that bit to 0, regardless of its current value.
Click to reveal answer
beginner
What does it mean to toggle a specific bit in a number?
Toggling a bit means changing it from 0 to 1 or from 1 to 0, flipping its current value.
Click to reveal answer
intermediate
In JavaScript, which operator is used to set a bit at position pos in number num?
Use the bitwise OR operator: num | (1 << pos) sets the bit at position pos.
Click to reveal answer
intermediate
How do you toggle a bit at position pos in JavaScript?
Use the bitwise XOR operator: num ^ (1 << pos) toggles the bit at position pos.
Click to reveal answer
Which operator is used to clear a bit at position pos in a number num?
Anum & ~(1 << pos)
Bnum | (1 << pos)
Cnum ^ (1 << pos)
Dnum << pos
✗ Incorrect
Clearing a bit uses AND with the negation of the bit mask: num & ~(1 << pos).
What is the result of toggling a bit that is currently 1?
AIt becomes 0
BIt stays 1
CIt becomes 2
DIt becomes -1
✗ Incorrect
Toggling flips the bit, so 1 becomes 0.
How do you set the 3rd bit (0-based) of number 5 (binary 0101)?
A5 ^ (1 << 3) = 1
B5 & ~(1 << 3) = 5
C5 | (1 << 3) = 13
D5 << 3 = 40
✗ Incorrect
Setting the 3rd bit of 5 (0101) sets bit 3: 0101 | 1000 = 1101 (decimal 13).
Which operation flips the bit at position pos?
AOR with mask
BXOR with mask
CAND with mask
DLeft shift
✗ Incorrect
XOR with mask flips the bit at the given position.
What does num & (1 << pos) check?
ASets bit at pos
BToggles bit at pos
CClears bit at pos
DIf bit at pos is set (1)
✗ Incorrect
AND with mask checks if the bit at position pos is 1.
Explain how to set, clear, and toggle a specific bit in a number using bitwise operators.
Think about how each operator affects bits: OR adds bits, AND removes bits, XOR flips bits.
You got /3 concepts.
Describe a real-life example where setting, clearing, or toggling bits might be useful.
Think about light switches or feature toggles in apps.
You got /4 concepts.
Practice
(1/5)
1. What does the operation num | (1 << k) do to the number num in terms of bits?
easy
A. Toggles the k-th bit of num
B. Sets the k-th bit of num to 1
C. Clears the k-th bit of num to 0
D. Checks if the k-th bit of num is set
Solution
Step 1: Understand bitwise OR with shifted 1
The expression 1 << k creates a number with only the k-th bit set to 1.
Step 2: Apply OR operation to set bit
Using num | (1 << k) sets the k-th bit of num to 1 without changing other bits.
Final Answer:
Sets the k-th bit of num to 1 -> Option B
Quick Check:
Bitwise OR with 1 at position k sets that bit [OK]
Hint: Use OR with shifted 1 to set a bit [OK]
Common Mistakes:
Confusing set with clear operation
Thinking it toggles the bit
Assuming it checks the bit instead
2. Which of the following JavaScript expressions correctly clears the k-th bit of a number num?
easy
A. num ^ (1 << k)
B. num | ~(1 << k)
C. num & (1 << k)
D. num & ~(1 << k)
Solution
Step 1: Understand clearing a bit
To clear the k-th bit, we want to set it to 0 while keeping others unchanged.
Step 2: Use AND with NOT mask
The mask ~(1 << k) has all bits 1 except the k-th bit 0. ANDing with this clears the k-th bit.
Final Answer:
num & ~(1 << k) -> Option D
Quick Check:
AND with inverted mask clears bit [OK]
Hint: AND with NOT of shifted 1 clears a bit [OK]
Common Mistakes:
Using OR instead of AND to clear bits
Forgetting to invert the mask
Using XOR which toggles instead of clears
3. What is the output of the following JavaScript code?
let num = 10; // binary 1010
let k = 1;
num = num ^ (1 << k);
console.log(num);
medium
A. 8
B. 10
C. 12
D. 14
Solution
Step 1: Understand initial values and bit positions
num = 10 (binary 1010), k = 1 means second bit from right (0-based).
Step 2: Apply XOR to toggle k-th bit
1 << 1 = 2 (binary 0010). XOR toggles bit 1: 1010 ^ 0010 = 1000 (binary), which is 8 decimal.
Final Answer:
8 -> Option A
Quick Check:
XOR toggles bit 1: 10 -> 8 [OK]
Hint: XOR toggles the bit at position k [OK]
Common Mistakes:
Confusing XOR with OR or AND
Miscounting bit positions (0-based)
Not converting binary to decimal correctly
4. The following code is intended to toggle the k-th bit of num. What is the error?
function toggleBit(num, k) {
return num & (1 << k);
}
console.log(toggleBit(5, 0));
medium
A. Function does not return a number
B. Incorrect bit shift direction
C. Using AND instead of XOR to toggle the bit
D. Missing parentheses around bit shift
Solution
Step 1: Identify intended toggle operation
Toggling a bit requires flipping it: 0 to 1 or 1 to 0.
Step 2: Check the operator used
The code uses AND (&) which masks bits, not toggles. XOR (^) is needed to toggle bits.
Final Answer:
Using AND instead of XOR to toggle the bit -> Option C
Quick Check:
Toggle needs XOR, not AND [OK]
Hint: Toggle bits with XOR, not AND [OK]
Common Mistakes:
Using AND or OR instead of XOR for toggle
Confusing toggle with set or clear
Ignoring operator precedence
5. You want to write a function updateBit(num, k, bitValue) that sets the k-th bit of num to bitValue (0 or 1). Which of the following implementations is correct?