ProgrammingHardTCS Digital · Capgemini

What will be the output of the following pseudocode? (integers are two's complement; ~ is bitwise NOT; >> is an arithmetic shift that keeps the sign)

What will be the output of the following pseudocode? (integers are two's complement; ~ is bitwise NOT; >> is an arithmetic shift that keeps the sign)
Integer a = 5, b = -1, c = -16
Print ~a, ~b, c >> 2
  1. A. -6 0 -4
  2. B. -5 1 -4
  3. C. -5 0 -4
  4. D. 250 0 -4

Correct answer: A. -6 0 -4

In two's complement ~x = −x − 1, so ~5 = −6 and ~(−1) = 0. An arithmetic right shift keeps the sign: −16 >> 2 = −4. Output -6 0 -4.

More Pseudocode and Code Tracing questions

Exams that ask this topic