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
Explain the key operation n = n & (n - 1) in Brian Kernighan's algorithm.
This operation removes the rightmost set bit from n. It works because subtracting 1 flips all bits after the rightmost set bit, and ANDing with n clears that bit.
Click to reveal answer
intermediate
Why is Brian Kernighan's algorithm more efficient than checking each bit individually?
Because 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
beginner
Write the base case condition for the Brian Kernighan algorithm loop.
The loop continues while n is not zero. When n becomes zero, all set bits have been counted and removed.
Click to reveal answer
intermediate
What is the time complexity of Brian Kernighan's algorithm for counting set bits?
O(k), where k is the number of set bits in the number, which is generally faster than O(log n) for checking all bits.
Click to reveal answer
What does the expression n & (n - 1) do in Brian Kernighan's algorithm?
AFlips all bits of n
BAdds 1 to n
CRemoves the rightmost set bit from n
DChecks if n is even
✗ Incorrect
The expression clears the rightmost set bit, which is the core step in Brian Kernighan's algorithm.
How many times does the loop run in Brian Kernighan's algorithm?
AEqual to the number of set bits in n
BEqual to the total number of bits in n
CAlways fixed at 32
DEqual to the number of zero bits in n
✗ Incorrect
The loop runs once for each set bit, making it efficient for sparse bit patterns.
What is the initial value of the count variable in Brian Kernighan's algorithm?
A0
B1
Cn
DUndefined
✗ Incorrect
Count starts at zero and increments each time a set bit is removed.
When does the Brian Kernighan's algorithm stop running?
AAfter 32 iterations
BWhen n becomes zero
CWhen count reaches n
DWhen n becomes negative
✗ Incorrect
The algorithm stops when all set bits are removed and n becomes zero.
Which of these is a benefit of Brian Kernighan's algorithm?
AIt counts zero bits
BIt uses recursion
CIt requires extra memory
DIt is faster for numbers with few set bits
✗ Incorrect
Because it loops only over set bits, it is faster when the number has few ones.
Describe how Brian Kernighan's algorithm counts the number of set bits in a number.
Think about how the number changes each time you remove a set bit.
You got /3 concepts.
Explain why Brian Kernighan's algorithm is more efficient than checking each bit one by one.
Consider how many times the loop runs compared to total bits.
You got /3 concepts.
Practice
(1/5)
1. What does the Brian Kernighan algorithm do when counting set bits in an integer?
easy
A. It counts the total number of digits in the integer
B. It converts the number to binary string and counts '0's
C. It shifts the bits to the right without changing the number
D. It repeatedly clears the least significant set bit until the number becomes zero
Solution
Step 1: Understand the algorithm's main operation
The Brian Kernighan algorithm works by clearing the least significant set bit in each iteration.
Step 2: Effect of clearing bits
Each clearing reduces the number of set bits by one until the number becomes zero.
Final Answer:
It repeatedly clears the least significant set bit until the number becomes zero -> Option D
Quick Check:
Clearing bits one by one counts set bits [OK]
Hint: Clear least set bit repeatedly to count set bits [OK]
Common Mistakes:
Counting total digits instead of set bits
Shifting bits without counting
Counting zeros instead of ones
2. Which of the following Java code snippets correctly implements the Brian Kernighan algorithm to count set bits?
easy
A. int count = 0; for(int i=0; i<32; i++) { if((n & (1 << i)) != 0) count++; }
B. int count = 0; while(n > 0) { n = n >> 1; count++; }
C. int count = 0; while(n > 0) { n = n & (n - 1); count++; }
D. int count = 0; while(n > 0) { n = n | (n - 1); count++; }
Solution
Step 1: Identify the correct bit clearing operation
The Brian Kernighan algorithm uses n = n & (n - 1) to clear the least significant set bit.
Step 2: Verify the loop and count increment
Loop runs while n > 0 and increments count each time a bit is cleared.
Final Answer:
int count = 0; while(n > 0) { n = n & (n - 1); count++; } -> Option C
Quick Check:
Use n = n & (n - 1) to clear bits [OK]
Hint: Use n = n & (n - 1) inside loop to count bits [OK]
Common Mistakes:
Using right shift instead of bit clearing
Using bitwise OR instead of AND
Counting bits by checking each bit manually
3. What is the output of the following Java code snippet?
int n = 13; // binary 1101
int count = 0;
while (n > 0) {
n = n & (n - 1);
count++;
}
System.out.println(count);
medium
A. 2
B. 3
C. 4
D. 5
Solution
Step 1: Convert 13 to binary and count set bits
13 in binary is 1101, which has three set bits (1,1,0,1).
Step 2: Apply Brian Kernighan algorithm iterations
Each iteration clears one set bit:
- Iteration 1: 1101 & 1100 = 1100
- Iteration 2: 1100 & 1011 = 1000
- Iteration 3: 1000 & 0111 = 0000
Loop runs 3 times, so count = 3.
Final Answer:
3 -> Option B
Quick Check:
Number of set bits in 13 = 3 [OK]
Hint: Count set bits in binary 1101 = 3 [OK]
Common Mistakes:
Counting total bits instead of set bits
Miscounting set bits in binary
Confusing bitwise AND with OR
4. Identify the error in the following Java code that attempts to count set bits using Brian Kernighan's algorithm:
int countSetBits(int n) {
int count = 0;
while (n != 0) {
n = n & (n + 1);
count++;
}
return count;
}
medium
A. Using n + 1 instead of n - 1 in bit clearing step
B. Incorrect loop condition should be n > 0
C. Missing initialization of count variable
D. Using bitwise OR instead of AND
Solution
Step 1: Check the bit clearing operation
The correct operation to clear the least significant set bit is n = n & (n - 1), not n & (n + 1).
Step 2: Effect of incorrect operation
Using n + 1 does not clear the least significant set bit and leads to incorrect counting or infinite loop.
Final Answer:
Using n + 1 instead of n - 1 in bit clearing step -> Option A
Quick Check:
Use n & (n - 1) to clear bits [OK]
Hint: Always use n & (n - 1) to clear bits [OK]
Common Mistakes:
Using n + 1 instead of n - 1
Wrong loop condition causing infinite loop
Confusing AND with OR operators
5. Given an array of integers, which Java method correctly uses Brian Kernighan's algorithm to return the total count of set bits in all elements?
int totalSetBits(int[] arr) {
int total = 0;
for (int num : arr) {
while (num > 0) {
num = num & (num - 1);
total++;
}
}
return total;
}
What is the output of totalSetBits(new int[]{3, 5, 7})?
hard
A. 7
B. 6
C. 8
D. 5
Solution
Step 1: Count set bits for each element
3 (binary 11) has 2 set bits, 5 (binary 101) has 2 set bits, 7 (binary 111) has 3 set bits.
Step 2: Sum all set bits
Total = 2 + 2 + 3 = 7.
Step 3: Verify code logic
The code correctly counts set bits for each number and sums them in total.
Final Answer:
7 -> Option A
Quick Check:
Sum of set bits in 3,5,7 = 7 [OK]
Hint: Sum set bits of each number using Brian Kernighan [OK]
Common Mistakes:
Miscounting set bits in numbers
Adding counts incorrectly
Using wrong bit clearing logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.