I'm using the following trick to iterate through the bits set of an int:
while (b != 0)
{
c = b & (0 - b);
//Do something...
b = b ^ c;
}
Taking as an example the number 4128 (binary 0001000000100000) this works fine because c's values are 32 and 4096.
However, instead of the actual values I would like the positions of those values, these being 5 and 12.
Is there an extra line of code that can be inserted into the loop that will return the position?
for-loop?Integer.numberOfTrailingZerosto get the bit-index.