#include <stdio.h> #include <stdint.h> int countSetBits(unsigned int n) { // Write your code here unsigned int counter; while(n) { if(n&1) counter++; n>>=1; } return counter; } int main() { int n; scanf("%d", &n); printf("%d", countSetBits(n)); return 0; }
n=5 //00000101n&10000010100000001-------------------00000001 --->//counter 1n>>=100000010n&10000001000000001--------------------00000000 ---> //No set bit so counter won't increasen>>=100000001n&10000000100000001----------------------00000001 --->//Counter 2
Test Cases
Test Results
Input
5
Expected Output
2