Click here to see the full article post
i&i-1 clears the last bit set in i. The number of bits set in i would therefore be, the number of bits set in i&i-1 plus the bit that the AND operation cleared. For eg, take i = 3 (11). i-1 = 2 (10). 3&2 = 2 (10). The answer would be, the number of bits set in 2(=1), plus 1(from the cleared bit) = 2.
@janani2 thanks for help! it is helpful
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.