ProgrammingHardGoldman Sachs OA · Amazon OA

What will be the output of the following pseudocode? (& is bitwise AND on 32-bit integers)

What will be the output of the following pseudocode? (& is bitwise AND on 32-bit integers)
Function pow4(Integer n)
    return n > 0 AND (n & (n - 1)) == 0 AND (n & 1431655765) != 0
End Function
Print pow4(16), pow4(32), pow4(1)
  1. A. true true false
  2. B. false false true
  3. C. true false true
  4. D. true false false

Correct answer: C. true false true

n & (n − 1) == 0 means n is a power of 2, and 1431655765 = 0101…0101 in binary keeps only the even bit positions, which is where powers of 4 have their 1-bit. 16 = 4², 32 = 2⁵ (odd position) fails, 1 = 4⁰ passes: true false true.

More Pseudocode and Code Tracing questions

Exams that ask this topic