Your question is Set Bits Optimization. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Intel Atom® x6000E firmware frequently inspects fixed-width control and status registers. Implement an optimized function that returns the number of set bits in a nonnegative integer represented with a specified number of bits.
The implementation should use parallel bit-counting operations rather than examining each bit individually. Treat value as an unsigned width-bit register value.
Implement count_set_bits(value, width).
value, a nonnegative integer, and width, an integer from 1 through 64.1 bits in the low width bits of value.0 <= value < 2 ** width.def count_set_bits(value, width):