The expression idx -= (idx & -idx)
sets the lowest bit in idx
whose value is 1 to 0.
For example, 10001010010 ==> 10001010000.
So the number of iterations in the loop above will be equal to the number of 1s in idx
.
BTW, a simpler way to perform this operation is with idx &= idx-1
.
The SWAR Algorithm is considered to be the most efficient way to count the number of 1s.