Pergunta de entrevista da empresa Cisco

What does the following function do: unsigned int doit(int x) { unsigned int z = 0; while (x &= (x-1)) z++; return z; }

Respostas da entrevista

Sigiloso

28 de mai. de 2010

This is the question with the unintentional bug in it. I ran through it 4 times and kept getting non-sensical answers. It was supposed to count the number of enabled bits in x, but it is off by one.

1

Sigiloso

27 de jan. de 2013

count no of ones each time it chops 1

Sigiloso

17 de abr. de 2013

doti() returns 0 if x is a power of 2: x -- doit(x) 1 -> 0 2 -> 0 3 -> 1 4 -> 0 8 -> 0