The goal is to write a program that returns number of 1 bits in a given number.

Examples

5 -> 2
1254 -> 6
56465 -> 8

Winner

The winning submission is the code which runs in the minimum time. You can assume that normal int value in your language can handle any number (no matter how large) and computes all the valid operators defined for integer in O(1).

If two pieces of code run in the same time the winner is the one implemented in fewer bytes.

Please provide some explanation how your code is computing the result.

\$\begingroup\$@gareth: that's not a duplicate, in that question binary operators were not allowed, while in mine you can use them. beside in that question he assumed numbers are small while in mine I assume int is large enough!\$\endgroup\$
– Ali1S232Jan 9 '12 at 23:36

\$\begingroup\$Runtime is not a meaningful selection criteria as this is highly dependent on the language used. Are you expecting answers in assembler?\$\endgroup\$
– gnibblerJan 9 '12 at 23:38

\$\begingroup\$I usually write my own codes in c++, it doesn't really matter which language you are using as while as I can implement the same algorithm in c++.\$\endgroup\$
– Ali1S232Jan 9 '12 at 23:47

\$\begingroup\$@Gajet then shouldn't you mention that in the question & add a c++ tag?\$\endgroup\$
– elssarJan 10 '12 at 7:19

\$\begingroup\$though I'm not big int part (caused by all div and mod parts), it sure works fast enough for int and long .\$\endgroup\$
– Ali1S232Jan 10 '12 at 0:05

\$\begingroup\$@Gajet you can use shift and mask (its dividing and mod by a power of 2) I edited to clarify\$\endgroup\$
– ratchet freakJan 10 '12 at 0:20

\$\begingroup\$I think a lookup table will be faster. For an 8 bit lookup table, you only need 4 lookups and 3 additions to do 32 bit. 16 bit lookup table will be slightly faster but much bigger.\$\endgroup\$
– gnibblerJan 10 '12 at 1:12

\$\begingroup\$@gnibbler then you get caching issues\$\endgroup\$
– ratchet freakJan 10 '12 at 2:49

scala:

computes on a 2Ghz single core for about 1.500.000 64bit Long values the number of bits set.
A straight forward recursive method, comparing the last bit, and shifting the number towards zero. The @tailrec is just a hint to the compiler to warn me, if it can't optimize the tail recursive call - it is not necessary, for the optimization to take place.

It's about 10times faster than the simplest method, using the library function:

\$\begingroup\$the other answers are much more faster, for example ratchet only computes 18 operations for 64bit integer, while you code need at least 128 operations for same size int.\$\endgroup\$
– Ali1S232Jan 10 '12 at 9:23

\$\begingroup\$Yes, I see - 10 M longs/s with bitcount as Javacode on the same machine. Btw.: I count 24 operations for ratchet, not 18.\$\endgroup\$
– user unknownJan 10 '12 at 10:06

\$\begingroup\$my bad! by the way java use the same algorithm as gnibbler suggested\$\endgroup\$
– Ali1S232Jan 10 '12 at 10:09

Your Answer

If this is an answer to a challenge…

…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.

…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

…Please make sure to answer the question and provide sufficient detail.

…Avoid asking for help, clarification or responding to other answers (use comments instead).