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. With x = 75, what is the value of y after the following assignment?
8-bit: x ← 75, y
y ← (x << 4) ∨ (x >> 4)
- A75
- B180
- C210
Show answer and explanation
Answer: B. 180
75 is 0100 1011 in binary. x << 4 moves the low 4 bits to the top, giving 1011 0000 = 176 (the 0100 shifted out is discarded), and x >> 4 moves the high 4 bits to the bottom, giving 0000 0100 = 4. Their OR is 1011 0100 = 180: the high and low 4 bits are swapped. Treating x << 4 as 75 × 16 = 1200 without limiting it to 8 bits would change the result.
- A. This assumes the swap leaves the value unchanged. The high and low 4 bits differ, so the value changes.
- B. Correct. Swapping the halves of 0100 1011 gives 1011 0100 = 180.
- C. This is 1101 0010, the 8 bits in reverse order.
Continue: 9 more Programming Basics (Pseudocode Tracing) questions