ProgrammingHardTCS NQT · Infosys pattern

Assume 32-bit int. What is printed?

Assume 32-bit int. What is printed?
int x = 0xFF;
int y = x & 0x0F;
int z = (x >> 4) & 0x0F;
printf("%d %d", y, z);
  1. A. 15 15
  2. B. 0 15
  3. C. 15 0
  4. D. 255 0

Correct answer: A. 15 15

0xFF & 0x0F isolates the low 4 bits (the nibble), giving 0xF=15. Shifting right by 4 first moves the high nibble into the low position; ANDing with 0x0F then isolates that nibble too, which for 0xFF is also 0xF=15 (since both nibbles of 0xFF are F).

More C Programming questions

Exams that ask this topic