Programming Basics (Pseudocode Tracing)
∧ is bitwise AND. What is the value of c after the following program runs?
integer: x ← 200, c ← 0
while (x ≠ 0)
x ← x ∧ (x − 1)
c ← c + 1
endwhile
- A3
- B5
- C8
Show answer and explanation
Answer: A. 3
The bitwise operation x ∧ (x − 1) clears the lowest 1 bit of x. In x − 1 the lowest 1 becomes 0 and all the 0s below it become 1, so the AND clears just that part. 200 = 11001000 becomes 11000000 = 192, then 10000000 = 128, then 0, so the loop runs 3 times. c ends up as the number of 1 bits in the binary form of x.
- A. Correct. 200 = 11001000 has three 1 bits.
- B. This is the number of 0 bits. The program clears the 1 bits one at a time.
- C. This is the 8-bit width. The loop runs once per 1 bit.
Continue: 9 more Programming Basics (Pseudocode Tracing) questions