Exercism c++ problem

Division and multiplication:

int countOnes(unsigned int x)
{
  int count = 0;

  while (x) {
    int tx = x / 2;
    int rx = 2 * tx;

    if (rx != x) count++;

    x = tx;
  }

  return count;
}

Or if we allowed, shift right and shift left:

    int tx = x >> 1;
    int rx = tx << 1;

a7

1 Like