#include <stdio.h>
int countSetBits(unsigned int n) {
// Write your code here
int cnt = 0;
for(unsigned int i = 0; i<32; i++){
if((n>>i) & 0x01) cnt++;
}
return cnt;
}
int main() {
int n;
scanf("%d", &n);
printf("%d", countSetBits(n));
return 0;
}
Solving Approach
Check each bit is 1 by anding with 1 after the number is right shifted i times while iterating through a loop running 32 times. increment count if the bit is 1.