Pergunta de entrevista da empresa Meta

Write a function that takes in an integer and returns the number of ones set in the binary representation.

Respostas da entrevista

Sigiloso

1 de jun. de 2009

count = 0 while(num) { num &= num-1; count++; } return count;

2

Sigiloso

24 de set. de 2009

I thought the question was asking how to convert the integer to binary code and output it.

1

Sigiloso

21 de fev. de 2010

function getOnes($int) { return substr_count(decbin($int), '1'); }

1

Sigiloso

7 de abr. de 2012

__builtin_popcount(num) :D