ProgrammingVery hardGoldman Sachs OA · Amazon OA

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

What will be the output of the following pseudocode? (& is AND, | is OR, << and >> are shifts)
Integer x = 43
Integer y = ((x & 170) >> 1) | ((x & 85) << 1)
Print y
  1. A. 43
  2. B. 86
  3. C. 21
  4. D. 23

Correct answer: D. 23

170 = 10101010 and 85 = 01010101 pick out the odd and even bit positions; shifting them towards each other swaps every adjacent pair of bits. 43 = 00101011 becomes 00010111 = 23.

More Pseudocode and Code Tracing questions

Exams that ask this topic