ProgrammingHardInfosys SP · Capgemini

What will be the output of the following pseudocode? (integers are 32-bit; & is AND, << and >> are shifts)

What will be the output of the following pseudocode? (integers are 32-bit; & is AND, << and >> are shifts)
Integer x = 45
Integer y = (x << 3) & 255
Integer z = (x << 3) >> 3
Print y, z
  1. A. 360 45
  2. B. 104 13
  3. C. 104 45
  4. D. 45 45

Correct answer: C. 104 45

45 << 3 = 360. Keeping only the low 8 bits: 360 − 256 = 104. Without that mask the value is not cut, so shifting back gives 45 again: 104 45.

More Pseudocode and Code Tracing questions

Exams that ask this topic