In C, which of the following code snippets correctly swaps the values of two integer variables a and b without using a third temporary variable, while avoiding undefined behaviour for distinct variables?

Difficulty: Medium

Correct Answer: a = a ^ b; b = a ^ b; a = a ^ b;

Explanation:


Introduction / Context:
Swapping two variables without using a third temporary variable is a classic interview and exam question in C. Several arithmetic or bitwise tricks are possible, but not all of them are safe or well defined. The most well known solution uses the XOR operator, which works reliably for distinct integer variables. This question asks you to identify the correct sequence of operations that performs the swap without introducing undefined behaviour for ordinary int variables a and b.


Given Data / Assumptions:

  • a and b are distinct int variables stored in memory locations that do not alias.
  • The goal is to swap their values without declaring a third variable.
  • We can use integer arithmetic and bitwise operators.
  • We must avoid undefined behaviour such as overflow in general cases.


Concept / Approach:
The XOR swap algorithm relies on the property that x ^ x equals zero and x ^ 0 equals x. The sequence a = a ^ b; b = a ^ b; a = a ^ b; effectively encodes both original values in combinations of XOR operations and then decodes them back in reversed positions. Unlike addition based methods, XOR does not overflow and is well defined for all int values. However, it should not be used when a and b are aliases of the same location, because that would collapse both values to zero.


Step-by-Step Solution:
Step 1: Start with original values: let a hold A and b hold B. Step 2: Execute a = a ^ b; so that a now holds A ^ B while b still holds B. Step 3: Execute b = a ^ b; which computes (A ^ B) ^ B. Because B ^ B equals 0 and A ^ 0 equals A, b now holds A. Step 4: Execute a = a ^ b; which computes (A ^ B) ^ A. By the same reasoning, a now holds B. Step 5: Conclude that the values have been swapped: a contains B and b contains A without using any temporary variable.


Verification / Alternative check:
You can verify the algebra using specific numeric examples, such as A equals 5 and B equals 9. Applying the XOR sequence yields the swapped values as expected. You can also reason that XOR is its own inverse, so applying it three times in this pattern restores the original values at opposite locations. The operations use only well defined bitwise behaviour on ints, so no overflow concerns arise.


Why Other Options Are Wrong:
Option B uses addition and subtraction, which may overflow when a and b are large in magnitude, leading to undefined behaviour in C. Option C applies a series of additions and subtractions that does not correctly restore the original values in all cases. Option D refers to a built in swap function, which does not exist in standard C; swap would need to be defined explicitly by the programmer.


Common Pitfalls:
One pitfall is to memorise the XOR trick without understanding its limitations, such as the aliasing case where a and b refer to the same memory location. Another is to prefer clever tricks over clear code; in practice, using a temporary variable often produces more readable code and the compiler can optimise it efficiently. For exam purposes, however, knowing the XOR pattern and the reasons why arithmetic based swaps can overflow is valuable.


Final Answer:
The correct swap without a third variable is performed by the XOR sequence a = a ^ b; b = a ^ b; a = a ^ b; which safely exchanges the values of distinct int variables.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion