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
  1. Ax = 10, y = 12
  2. Bx = 6, y = 12
  3. 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.
Practice this topic Back to topic

Related calculators

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

More Programming Basics (Pseudocode Tracing) questions

  1. 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?
  2. ∧ is bitwise AND. What is the value of c after the following program runs?
  3. 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?
  4. The following procedure bubble is a bubble sort that sorts the integer array data in ascending order. swap(data, j, k) is a procedure that swaps the values of data[j] and data[k]. Array indices start at 1. What should replace 【a】 in the program?
  5. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. The following program finds in lo the index of the first value that is at least x in the integer array data, which is sorted in ascending order (indices start at 1). x is no greater than the last element of data. What should replace 【a】 in the program?