ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
unsigned v = 0;
const char *b = "10110";
while (*b) v = (v << 1) | (*b++ - '0');
printf("%u", v);
  1. A. 11
  2. B. 13
  3. C. 44
  4. D. 22

Correct answer: D. 22

Each step shifts the value left and appends the next binary digit (character minus '0'): 1, 10, 101, 1011, 10110 = 22. '13' reads the digits in reverse order (01101); '44' shifts once too often.

More C Programming questions

Exams that ask this topic