#include <stdio.h>
int countSetBits(unsigned int n) {
// Write your code here
int count=0,pos=0;
for(pos=31;pos>=0;pos--){
if(n>>pos&1){
count++;
}
}
return count;
}
int main() {
int n;
scanf("%d", &n);
printf("%d", countSetBits(n));
return 0;
}
int count=0,pos=0;
for(pos=31;pos>=0;pos--){
if(n>>pos&1){
count++;
}
}
return count;
unsigned int is 32bits
1. i am iterating to MSB(31) to LSB(0).
2. n>>pos :- it right shifting pos to 0 th postion
3. &1 :- checking the bit set or not.
4. if the bit is set increment the count.
Input
5
Expected Output
2