Pergunta de entrevista da empresa Qualcomm

Count the 1s in the number

Respostas da entrevista

Sigiloso

5 de abr. de 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

Sigiloso

20 de abr. de 2019

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

Sigiloso

27 de out. de 2011

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