38. Bitwise Rotation and Counting-I

Question.6

Two developers count set bits in an 8-bit register:

Developer A — check every bit:

int count = 0;
for (int i = 0; i < 8; i++) {
   count += (val >> i) & 1;
}

Developer B — clear lowest set bit each iteration:

int count = 0;
while (val) {
   val &= (val - 1);
   count++;
}

Which statement is correct?

Need Help? Refer to the Quick Guide below

Select Answer