Qualcomm Interview Question

Count the 1s in the number

Interview Answers

Anonymous

Apr 5, 2010

#define INTSIZE(16) int ones_counter(unsigned int num) { unsigned int counter = 0; for (i = 0; i < INTSIZE; ++i) { if ((1 << i) & num) counter++; } return (counter); }

4

Anonymous

Apr 20, 2019

int countsetbits(int n) { int count; while(n) { count+=n&1; n>>=1; } return count; }

Anonymous

Oct 27, 2011

int cnt = 0; while(n) { if(n & (n-1)) cnt++; n &= n-1; } return cnt;