ProgrammingHardGoldman Sachs OA · Infosys SP

What will be the output of the following pseudocode? (~ is bitwise NOT, & is AND, | is OR, >> is right shift)

What will be the output of the following pseudocode? (~ is bitwise NOT, & is AND, | is OR, >> is right shift)
Integer x = 11
Integer a = (~x) & (x + 1)
Integer y = 37
y = y | (y >> 1)
y = y | (y >> 2)
y = y | (y >> 4)
Print a, y
  1. A. 1 37
  2. B. 4 63
  3. C. 4 64
  4. D. 8 63

Correct answer: B. 4 63

(~x) & (x + 1) isolates the lowest 0-bit of x: 11 = 1011, so it is 0100 = 4. OR-ing y with its shifts copies the top 1-bit into every lower position: 37 = 100101 becomes 111111 = 63. Output 4 63.

More Pseudocode and Code Tracing questions

Exams that ask this topic