ProgrammingVery hardGoldman Sachs OA · Amazon OA

What will be the output of the following pseudocode? (All variables are integers, so / is integer division; & is AND, ^ is XOR, | is OR, >> is right shift; −x is two's complement)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division; & is AND, ^ is XOR, | is OR, >> is right shift; −x is two's complement)
Integer x = 156
Integer small = x & (-x)
Integer ripple = x + small
Integer ones = x ^ ripple
ones = (ones >> 2) / small
Print ripple | ones
  1. A. 157
  2. B. 184
  3. C. 172
  4. D. 163

Correct answer: D. 163

This is the classic 'next larger number with the same number of 1-bits'. 156 = 10011100: small = 4, ripple = 160 = 10100000, and the leftover 1's (ones = 3) are put back at the right: 10100011 = 163.

More Pseudocode and Code Tracing questions

Exams that ask this topic