ProgrammingHardInfosys SP · TCS Digital

What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift)

What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift)
Integer x = 7, m = 1
while ((x & m) != 0)
    x = x ^ m
    m = m << 1
end while
x = x ^ m
Integer y = 12
m = 1
while ((y & m) != 0)
    y = y ^ m
    m = m << 1
end while
y = y ^ m
Print x, y
  1. A. 15 13
  2. B. 8 13
  3. C. 6 11
  4. D. 8 12

Correct answer: B. 8 13

This adds 1 using only bit operations: it clears the trailing 1's and sets the next 0. 7 = 0111 → 1000 = 8; 12 = 1100 has no trailing 1's, so only the last bit is set: 13. Output 8 13.

More Pseudocode and Code Tracing questions

Exams that ask this topic