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);
}- A. 8
- B. 4
- C. 23
- 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
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { return atoi((const char const )a)…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { const int x = a, y = b; if (x[0] != y[0])…
- What is the digit sum of 2^15 = 32768? The recursion below computes it. Assume int is 32 bits. What is printed? c #include <stdio.h> int…
- How many subsets of {1, 2, 3} (the empty set included) have a sum divisible by 3? The recursion below counts them. What is printed? c…
- How many bit positions are zero in both 0xAAAA and 0x5555 within 16 bits? Count them with this program. c #include <stdint.h> #include…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = 12; printf("%u", (x << 3) + (x << 1)); }