Count Set Bits in an Integer

Code

#include <stdio.h>

int main() {
    unsigned int n;
    int count = 0;

    scanf("%u", &n);

    while (n) {
        n &= (n - 1);
        count++;
    }

    printf("%d", count);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

5

Expected Output

2