ProgrammingVery hardProduct company OA

What is printed?

What is printed?
unsigned next(unsigned x) {
    unsigned c = x & -x, r = x + c;
    return (((r ^ x) >> 2) / c) | r;
}
int main(void) { printf("%u %u", next(6), next(11)); }
  1. A. 7 12
  2. B. 10 13
  3. C. 9 13
  4. D. 12 14

Correct answer: C. 9 13

This is Gosper's hack: it returns the next larger number with the same count of 1 bits. 6 = 0110 → 1001 = 9 (two bits set). 11 = 1011 → 1101 = 13 (three bits set). '12 14' just shifts or adds without keeping the bit count.

More C Programming questions

Exams that ask this topic