ProgrammingHardGATE CS · TCS NQT

What is printed?

What is printed?
#include <stdio.h>
int clz16(unsigned short x) {
    int n = 0;
    while (!(x & 0x8000u)) {
        x <<= 1;
        n++;
    }
    return n;
}
int main(void) {
    printf("%d", clz16(0x0F00));
}
  1. A. 8
  2. B. 4
  3. C. 3
  4. D. 5

Correct answer: B. 4

Shifting 0x0F00 left until bit 15 is set takes four steps: 0x1E00, 0x3C00, 0x7800, 0xF000. Each step is stored back into an unsigned short, so no bits go past 16.

More C Programming questions

Exams that ask this topic