Pseudo-code · Question 13 · Hard

What will be the output of the following pseudocode? (Note: &, | and ^ are bitwise AND, OR and XOR.)

Integer n, c
Set n = 109, c = 0
while (n != 0)
    n = n & (n - 1)
    c = c + 1
end while
Print c
  1. 5
  2. 7
  3. 4
  4. 6
Answer and explanation

Answer: A. 5

Explanation: n & (n - 1) clears the lowest 1-bit of n, so the loop counts the 1-bits. 109 = 1101101 in binary, which has five 1-bits.

Previous: What will be the output of the following pseudocode? · Next: What will be the output of the following pseudocode? (Note: for-loop bounds…

Practise pseudo-code

Try a fresh set of pseudo-code questions with an explanation after every answer, free and without signing up: Pseudo-code practice. Or sit a full placement mock test.

More pseudo-code questions

More technical & programming practice

C Programming · Java · Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge