What is this algorithm doing?
For a value of n
, the number of bits that are on in n
is added to c
.
0x01
represents the value 1
. It is being used as a bit mask to check whether the lowest bit of m
is on. If it is, then it increments c
. This is the bit-counting part.
Then, m
is right-shifted once to move the next bit down into the position of LSB (least significant bit). The while(m)
will stop once m
is equal to 0
; that is, when m
has no more bits on.
And the algorithmic complexity depends on what you're doing with n
. If you're decrementing it, then your algorithm is O(n)
since the inner section (the while(m)
) is constant (since there is a maximum number of bits in an integer, and that is probably 32).
The complexity is probably O(n * 32)
, but since constants are taken out of Big-Oh notation, you end up with O(n)
.