ProgrammingHardProduct company OA

Assume 32-bit unsigned int. What is printed?

Assume 32-bit unsigned int. What is printed?
unsigned mask(int lo, int hi) { return ((1u << (hi - lo + 1)) - 1) << lo; }
int main(void) { printf("%X %X", mask(4, 7), mask(0, 3) | mask(8, 11)); }
  1. A. F0 F0F
  2. B. F 0F0F
  3. C. 70 F0F
  4. D. F0 FFF

Correct answer: A. F0 F0F

(1 << k) − 1 is a run of k ones; shifting it left by lo places the run at bits lo to hi. mask(4, 7) = 0xF0. mask(0, 3) = 0x00F and mask(8, 11) = 0xF00 combine to 0xF0F; 'FFF' wrongly fills bits 4 to 7 too.

More C Programming questions

Exams that ask this topic