30. Count Set Bits in an Integer

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

int size;
int countSetBits(unsigned int n)
{
    size=sizeof(n)*8;
    int count;
    for(int i=0;i<size;i++)
    {
            if((n>>i)&0x01)
            {
                count++;
            }
    }
    // Write your code here
    return count;
}

int main() {
    int n;
    scanf("%d", &n);
    printf("%d", countSetBits(n));
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote