Count Set Bits in an Integer

Code

#include <stdio.h>
#include <stdint.h>

int countSetBits(unsigned int n) {
    // Write your code here

    uint32_t result1 = n; 
    uint32_t result = 0x00; 
    uint32_t len = 0x00; 
    while (result1 > 0) 
    { 
        if(result1 & 1)
        {
            len++; 
        }
        result1 = result1 >> 1;   
    } 
    return len;
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

5

Expected Output

2