Programming Basics (Pseudocode Tracing)
⊕ is bitwise exclusive OR. What are the values of x and y after the following program runs?
integer: x ← 12, y ← 10
x ← x ⊕ y
y ← x ⊕ y
x ← x ⊕ y
- Ax = 10, y = 12
- Bx = 6, y = 12
- Cx = 12, y = 10
Show answer and explanation
Answer: A. x = 10, y = 12
In binary, 12 = 1100 and 10 = 1010. The first assignment gives x = 1100 ⊕ 1010 = 0110 = 6, the second y = 0110 ⊕ 1010 = 1100 = 12, and the third x = 0110 ⊕ 1100 = 1010 = 10. This swaps the values without a temporary variable, using the fact that XOR with the same value twice restores the original (a ⊕ b ⊕ b = a).
- A. Correct. Three exclusive ORs swap x and y.
- B. These are the values after the second assignment.
- C. This assumes the values are unchanged. They are actually swapped.
Continue: 9 more Programming Basics (Pseudocode Tracing) questions