Problem 808. Hamming Weight - Fast
Solution Stats
Solution Comments
-
1 Comment
1. This solution is much faster on re-invocation than the one without the persistent num_ones variable. Unless of course it is performed on a much larger (than num_ones) array of 32-bit integer.
2. It is essential to have the statement
x= double(x);
The reason for this is that the floor() function has a problem with precision. If can fail with 32-bit integer that are close to 2^32.
For instance, consider this Matlab code and system response:
>> p=uint32(4294946031)
p =
uint32
4294946031
>> floor(p/65536)
ans =
uint32
65536
Problem Recent Solvers10
Suggested Problems
-
6514 Solvers
-
Find best placement for ordered dominoes (harder)
262 Solvers
-
903 Solvers
-
Construct an index vector from two input vectors in vectorized fashion
266 Solvers
-
Generate N equally spaced intervals between -L and L
710 Solvers
More from this Author255
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!