ProgrammingModerateInfosys SP · TCS NQT

What will be the output of the following pseudocode? (| is OR, & is AND, << and >> are shifts)

What will be the output of the following pseudocode? (| is OR, & is AND, << and >> are shifts)
Integer x = 1, r = 0
for (each i from 1 to 8)
    r = (r << 1) | (x & 1)
    x = x >> 1
end for
Print r
  1. A. 128
  2. B. 1
  3. C. 256
  4. D. 2

Correct answer: A. 128

The loop reverses the lowest 8 bits: 00000001 becomes 10000000 = 128.

More Pseudocode and Code Tracing questions

Exams that ask this topic