Bird
0
0
DSA Cprogramming~20 mins

Count Set Bits Brian Kernighan Algorithm in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Set Bits Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Brian Kernighan's Algorithm for Counting Set Bits
What is the output of the following C code that counts set bits using Brian Kernighan's algorithm?
DSA C
int countSetBits(int n) {
    int count = 0;
    while (n) {
        n = n & (n - 1);
        count++;
    }
    return count;
}

int main() {
    int num = 29;
    int result = countSetBits(num);
    printf("%d\n", result);
    return 0;
}
A5
B3
C4
D6
Attempts:
2 left
💡 Hint
Count how many times the loop runs by clearing the rightmost set bit each time.
Predict Output
intermediate
2:00remaining
Result of Counting Set Bits for Zero Input
What will be the output when the input number is zero in the Brian Kernighan's set bit counting function?
DSA C
int countSetBits(int n) {
    int count = 0;
    while (n) {
        n = n & (n - 1);
        count++;
    }
    return count;
}

int main() {
    int num = 0;
    int result = countSetBits(num);
    printf("%d\n", result);
    return 0;
}
A0
BUndefined behavior
C1
DInfinite loop
Attempts:
2 left
💡 Hint
Consider how many set bits zero has.
🔧 Debug
advanced
2:00remaining
Identify the Error in Brian Kernighan's Algorithm Implementation
What error will the following code produce when compiled and run?
DSA C
int countSetBits(int n) {
    int count = 0;
    while (n != 0) {
        n = n & n - 1;
        count++;
    }
    return count;
}

int main() {
    int num = 15;
    int result = countSetBits(num);
    printf("%d\n", result);
    return 0;
}
ACompilation error due to operator precedence
BRuns correctly and outputs 4
CRuntime error due to invalid memory access
DInfinite loop
Attempts:
2 left
💡 Hint
Operator precedence: subtraction (-) has higher precedence than bitwise AND (&).
Predict Output
advanced
2:00remaining
Output of Counting Set Bits for a Large Number
What is the output of the following code when counting set bits of 1023 using Brian Kernighan's algorithm?
DSA C
int countSetBits(int n) {
    int count = 0;
    while (n) {
        n = n & (n - 1);
        count++;
    }
    return count;
}

int main() {
    int num = 1023;
    int result = countSetBits(num);
    printf("%d\n", result);
    return 0;
}
A11
B9
C8
D10
Attempts:
2 left
💡 Hint
1023 in binary is all ones in the first 10 bits.
🧠 Conceptual
expert
2:00remaining
Why Brian Kernighan's Algorithm is Efficient for Counting Set Bits
Which statement best explains why Brian Kernighan's algorithm is more efficient than checking each bit individually?
AIt reduces the number of iterations to the number of set bits instead of total bits.
BIt uses recursion to count bits faster than loops.
CIt converts the number to a string and counts '1' characters.
DIt uses a lookup table for each byte to count bits.
Attempts:
2 left
💡 Hint
Think about how many times the loop runs compared to total bits.