ProgrammingHardInfosys SP · Amazon OA

What will be the output of the following pseudocode? (| is bitwise OR)

What will be the output of the following pseudocode? (| is bitwise OR)
Integer x = 0, c = 0
while (x < 1000)
    x = x | (x + 1)
    c = c + 1
end while
Print c, x
  1. A. 9 511
  2. B. 10 1024
  3. C. 10 1023
  4. D. 11 2047

Correct answer: C. 10 1023

x | (x + 1) sets the lowest 0 bit, so x runs 1, 3, 7, 15, …, 2^c − 1. The first such value ≥ 1000 is 1023 = 2^10 − 1, reached after 10 steps: 10 1023.

More Pseudocode and Code Tracing questions

Exams that ask this topic