#include <stdio.h> int countSetBits(unsigned int n) { int set_bits = 0; int num_bits = 8 * sizeof(n); int i; for(i = 0; i < num_bits; i++){ if(n & (0x1 << i)){ set_bits++; } } return set_bits; } int main() { int n; scanf("%d", &n); printf("%d", countSetBits(n)); return 0; }
Test Cases
Test Results
Input
5
Expected Output
2