ProgrammingVery hardGoldman Sachs OA · Amazon OA

What will be the output of the following pseudocode? (& is bitwise AND)

What will be the output of the following pseudocode? (& is bitwise AND)
Integer mask = 13, s, c = 0
s = mask
while (s > 0)
    c = c + 1
    s = (s - 1) & mask
end while
Print c
  1. A. 7
  2. B. 13
  3. C. 8
  4. D. 3

Correct answer: A. 7

(s − 1) & mask steps through the non-empty sub-masks of mask in decreasing order. 13 = 1101 has 3 set bits, so it has 2³ − 1 = 7 non-empty sub-masks: 13, 12, 9, 8, 5, 4, 1.

More Pseudocode and Code Tracing questions

Exams that ask this topic