Explaination of exchanging values of two parameters via XOR

来源:互联网 发布:淘宝母婴用品货源 编辑:程序博客网 时间:2024/06/11 08:18

The intuitive way of exchanging values of two parameters is applying a third temporary parameter, for example, assume a and b are of type int, to exchange their values, we tend to define a temporary integer t, which plays the role of carrier, to fulfill the aim.

Pseudo code is as follows:

t = a; a = b; b = t;


However, there exists way to exchange values of two parameter involving none other parameter. The secret is the XOR binary operation. Let's review some results. Let a and b denote two binary parameters, that's their values can only be 0 or 1; the following table is just from definition:

|------------------------------|

|  a  |  b  |  c = a XOR b  |

|------------------------------|

|  0  |  0  |          0             |

|------------------------------|

|  0  |  1  |          1             |

|------------------------------|

|  1  |  0  |          1             |

|------------------------------|

|  1  |  1  |          0             |

|------------------------------|


There are two several cases:

|------------------------------|

|  a  |  a  |  c = a XOR a  |

|------------------------------|

|  0  |  0  |          0             |

|------------------------------|

|  1  |  1  |          0             |

|------------------------------|

That's a XOR a = 0


|------------------------------|

|  a  |  0  |  c = a XOR 0  |

|------------------------------|

|  0  |  0  |          0             |

|------------------------------|

|  1  |  0  |          1             |

|------------------------------|

That's a XOR 0 = a


|------------------------------|

|  a  |  1  |  c = a XOR 1  |

|------------------------------|

|  0  |  1  |          1             |

|------------------------------|

|  1  |  1  |          0             |

|------------------------------|


That's a XOR 1 = ~a


Armed with the above investigation, we can begin now. First let's expose the formula to satisfying the requirement in pseudo code :

x = x ^ y; // (1)

y = x ^ y; // (2)

x = x ^ y; // (3)


First, let's check in (2), y gets the original value of x. To make it more clear, let re-write (1) in x[1] = x ^ y, and (2) in the form of y[1] = x ^ y[1]. But actually in (2), x is sincerely x[1], that's:

x[1] = x ^ y;

y[1] = x[1] ^ y;

we do a substitution, it becomes y[1] = (x ^ y) ^ y.


We just declare that (x ^ y) ^ y = x ^ (y ^ y), anyone with any interests can prove it. Let's simplify x ^ (y ^ y). Since y ^ y = 0, so x ^ (y ^ y) = x ^ 0. However, we know x ^ 0 = x, so it becomes y[1] = x; we can rewrite it as y = x. Up to this stage, we finished part of our work.


Let's continue, we rewrite (3), x[2] = x[1] ^ y[1]; First we know in this statement, value of y[1] is sincerely the original value of x, so we simplify it as x[2] = x[1] ^ x. We already know x[1] = x ^ y, so it becomes x[2] = (x ^ y) ^ x.


We won't prove that (x ^ y) ^ x = (x ^ x) ^ y, but it does. It's easy to deduce that (x ^ x) ^ y = y, that's x[2] = y. When we rewrite it as x = y, we know we finished all the work.