Programming Basics (Pseudocode Tracing)
x and y are 8-bit unsigned integers. << and >> are logical shifts: vacated bits are filled with 0, and bits shifted out of the 8 bits are discarded. ∨ is bitwise OR. What is the value of y after the following program runs?
8-bit: x ← 13, y
y ← (x << 2) ∨ (x >> 1)
- A27
- B54
- C58
Show answer and explanation
Answer: B. 54
13 is 00001101 in binary. x << 2 shifts it 2 bits left to 00110100 = 52, and x >> 1 shifts it 1 bit right to 00000110 = 6 (the lowest 1 shifted out is discarded). Their OR is 00110110 = 54. 52 and 6 both have the bit for 4 set, so the OR differs from the sum 52 + 6 = 58.
- A. This shifts in the opposite directions: (x >> 2) ∨ (x << 1) = 3 ∨ 26.
- B. Correct. 00110100 ∨ 00000110 = 00110110 = 54.
- C. This adds instead of ORing. They share a 1 bit (the 4s bit), so the results differ.
Continue: 9 more Programming Basics (Pseudocode Tracing) questions