ProgrammingHardGATE CS · TCS NQT

How many leading zero bits does the 16-bit value 0x0123 have? Count them with this program.

How many leading zero bits does the 16-bit value 0x0123 have? Count them with this program.
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint16_t x = 0x0123;
    int c = 0;
    for (int i = 15; i >= 0 && !((x >> i) & 1u); i--)
        c++;
    printf("%d", c);
}
  1. A. 8
  2. B. 4
  3. C. 23
  4. D. 7

Correct answer: D. 7

0x0123 = 0000 0001 0010 0011, so the loop counts bits 15 down to 9 and stops at bit 8: 7 zeros. 8 confuses the bit index with the count, 4 counts only the leading zero hex digit, and 23 assumes a 32-bit width although the loop starts at bit 15.

More C Programming questions

Exams that ask this topic