What does the Brian Kernighan Algorithm do in the context of counting set bits?
It counts the number of 1s (set bits) in the binary representation of a number by repeatedly turning off the rightmost set bit until the number becomes zero.
Click to reveal answer
intermediate
How does the Brian Kernighan Algorithm reduce the number of operations compared to checking each bit individually?
It only loops as many times as there are set bits, not the total number of bits, making it faster especially for numbers with few set bits.
Click to reveal answer
intermediate
Explain the key operation n = n & (n - 1) in the Brian Kernighan Algorithm.
This operation removes the rightmost set bit from n by turning it off, effectively reducing the count of set bits by one each time it is executed.
Click to reveal answer
intermediate
What is the time complexity of the Brian Kernighan Algorithm for counting set bits?
O(k), where k is the number of set bits in the number, which is usually less than the total number of bits.
Click to reveal answer
beginner
Provide a simple JavaScript function using Brian Kernighan Algorithm to count set bits.
function countSetBits(n) {
let count = 0;
while (n !== 0) {
n = n & (n - 1);
count++;
}
return count;
}
Click to reveal answer
What does the expression n & (n - 1) do in the Brian Kernighan Algorithm?
AInverts all bits of n
BTurns on the rightmost unset bit of n
CTurns off the rightmost set bit of n
DShifts n to the right by one bit
✗ Incorrect
The expression n & (n - 1) removes the rightmost set bit from n by turning it off.
What is the main advantage of Brian Kernighan Algorithm over checking each bit individually?
AIt only loops as many times as there are set bits
BIt uses less memory
CIt works only for even numbers
DIt counts unset bits instead
✗ Incorrect
The algorithm loops only for the number of set bits, making it faster than checking every bit.
If a number has 3 set bits, how many times will the Brian Kernighan Algorithm loop?
ANumber of total bits
BAlways 1
CDepends on the number's size
D3
✗ Incorrect
The loop runs exactly as many times as there are set bits.
What is the initial value of the count variable in the Brian Kernighan Algorithm?
A1
B0
CNumber of bits in n
DUndefined
✗ Incorrect
Count starts at 0 and increments each time a set bit is removed.
Which of the following is a correct output of countSetBits(13) using Brian Kernighan Algorithm?
A3
B4
C2
D5
✗ Incorrect
13 in binary is 1101, which has 3 set bits.
Describe how the Brian Kernighan Algorithm counts set bits in a number.
Think about how the algorithm reduces the number by turning off bits.
You got /4 concepts.
Explain why Brian Kernighan Algorithm is more efficient than checking each bit one by one.
Focus on how many times the loop runs compared to total bits.
You got /4 concepts.
Practice
(1/5)
1. What does the Brian Kernighan algorithm do when counting set bits in a number?
easy
A. It converts the number to a string and counts characters
B. It counts the total number of digits in the number
C. It repeatedly clears the least significant set bit until the number becomes zero
D. It shifts the number to the right and counts zeros
Solution
Step 1: Understand the algorithm's main operation
The algorithm clears the least significant set bit by doing n = n & (n-1) repeatedly.
Step 2: Effect of clearing bits
Each operation removes one set bit, counting how many times this happens gives the total set bits.
Final Answer:
It repeatedly clears the least significant set bit until the number becomes zero -> Option C
Quick Check:
Clearing bits counts set bits [OK]
Hint: Clear least set bit repeatedly to count set bits [OK]
Common Mistakes:
Counting total digits instead of set bits
Counting zeros instead of ones
Using string length instead of bit operations
2. Which of the following is the correct syntax to clear the least significant set bit in JavaScript using Brian Kernighan's method?
easy
A. n = n | (n - 1);
B. n = n & (n - 1);
C. n = n & (n + 1);
D. n = n ^ (n - 1);
Solution
Step 1: Identify the bit clearing operation
The correct operation to clear the least significant set bit is n = n & (n - 1).
Step 2: Verify other options
Other options do not clear the least significant set bit correctly.
Final Answer:
n = n & (n - 1); -> Option B
Quick Check:
Use AND with (n-1) to clear bit [OK]
Hint: Use n & (n-1) to clear least set bit [OK]
Common Mistakes:
Using OR or XOR instead of AND
Adding instead of subtracting 1
Confusing bitwise operators
3. What is the output of the following JavaScript code?
function countSetBits(n) {
let count = 0;
while (n > 0) {
n = n & (n - 1);
count++;
}
return count;
}
console.log(countSetBits(13));
medium
A. 3
B. 2
C. 4
D. 5
Solution
Step 1: Convert 13 to binary
13 in binary is 1101, which has three set bits (1s).
Step 2: Apply Brian Kernighan's algorithm
Each loop clears one set bit, so count increments 3 times before n becomes 0.
Final Answer:
3 -> Option A
Quick Check:
13 (1101) has 3 set bits [OK]
Hint: Count set bits by clearing one bit each loop [OK]
Common Mistakes:
Counting total bits instead of set bits
Miscounting binary representation
Off-by-one errors in loop
4. Identify the error in the following code snippet that uses Brian Kernighan's algorithm:
function countSetBits(n) {
let count = 0;
while (n != 0) {
n = n & (n + 1);
count++;
}
return count;
}
medium
A. Not initializing count to 1
B. Using while loop instead of for loop
C. Missing return statement
D. Using n & (n + 1) instead of n & (n - 1)
Solution
Step 1: Check the bit clearing operation
The code uses n = n & (n + 1), which does not clear the least significant set bit correctly.
Step 2: Correct operation
The correct operation is n = n & (n - 1) to clear the least significant set bit.
Final Answer:
Using n & (n + 1) instead of n & (n - 1) -> Option D
Quick Check:
Use n & (n-1) to clear bits [OK]
Hint: Use n & (n-1), not n & (n+1) [OK]
Common Mistakes:
Using n & (n + 1) which is incorrect
Confusing loop types
Forgetting to return count
5. Given an array of integers, which code snippet correctly uses Brian Kernighan's algorithm to find the total count of set bits in all elements?
const arr = [3, 5, 7];
// Choose the correct code to count all set bits
A) let total = 0;
for(let n of arr) {
while(n > 0) {
n = n & (n - 1);
total++;
}
}
console.log(total);
B) let total = 0;
for(let n of arr) {
total += n.toString(2).length;
}
console.log(total);
C) let total = 0;
for(let n of arr) {
while(n > 0) {
n = n | (n - 1);
total++;
}
}
console.log(total);
D) let total = 0;
for(let n of arr) {
total += n & (n - 1);
}
console.log(total);
hard
A. Correctly counts set bits using Brian Kernighan's method
B. Counts total binary digits, not set bits
C. Uses OR instead of AND, incorrect bit clearing
D. Adds bitwise AND result directly, incorrect counting
Solution
Step 1: Identify the correct implementation
The implementation using while(n > 0) { n = n & (n - 1); total++; } correctly applies Brian Kernighan's algorithm by clearing the least significant set bit repeatedly for each array element and incrementing the total count each time.
Step 2: Eliminate incorrect options
The option using n.toString(2).length counts all bits including zeros; using n | (n - 1) does not clear set bits correctly; adding n & (n - 1) directly does not count the number of set bits.
Final Answer:
Correctly counts set bits using Brian Kernighan's method -> Option A
Quick Check:
Clear bits and count in loop [OK]
Hint: Loop clearing bits per number to sum counts [OK]
Common Mistakes:
Counting total binary length instead of set bits
Using OR instead of AND for clearing bits
Adding bitwise results instead of counts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.