2
1
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.
2
Duplicate: Print the amount of ones in a binary number without using bitwise operators
– Gareth – 2012-01-09T23:14:30.3431@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! – Ali1S232 – 2012-01-09T23:36:21.447
Runtime is not a meaningful selection criteria as this is highly dependent on the language used. Are you expecting answers in assembler? – gnibbler – 2012-01-09T23:38:17.997
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++. – Ali1S232 – 2012-01-09T23:47:17.263
@Gajet then shouldn't you mention that in the question & add a c++ tag? – elssar – 2012-01-10T07:19:16.860
@elssar as I said it's not important that your code is written in c++. I can almost convert any code to c++, since CPU can run any code in any language! as I said I just need explanation how some algorithms work. – Ali1S232 – 2012-01-10T09:19:34.450