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 |
|------------------------------|
|------------------------------|
| 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.
- Explaination of exchanging values of two parameters via XOR
- explaination of mysql/bin/ file
- Personnal explaination of Appcoef.m
- The program of choosing TWO parameters
- [VBS] Compare values of two arrays
- 【LeetCode-421】Maximum XOR of Two Numbers in an Array
- [leetcode]421. Maximum XOR of Two Numbers in an Array
- 421. Maximum XOR of Two Numbers in an Array详解
- LeetCode #421: Maximum XOR of Two Numbers in an Array
- 421. Maximum XOR of Two Numbers in an Array
- 421. Maximum XOR of Two Numbers in an Array
- 421. Maximum XOR of Two Numbers in an Array
- 421. Maximum XOR of Two Numbers in an Array
- 【LeetCode】 421. Maximum XOR of Two Numbers in an Array
- 421. Maximum XOR of Two Numbers in an Array
- [leetcode]421. Maximum XOR of Two Numbers in an Array
- Trie (3) -- Maximum XOR of Two Numbers in an Array
- 421. Maximum XOR of Two Numbers in an Array
- SQL2008单一的Windows身份验证改为混合模式身份验证(部分是转载)
- 黑马程序员_图形化界面
- c 获取文件 MD5 值
- 快速排序
- ASP.NET MVC 3 JSON 返回时间格式处理
- Explaination of exchanging values of two parameters via XOR
- 在项目中选择串口号的方法
- 面试题整理(二)
- 介绍一个简单的串口类,同一时间只能读或写
- LVS-DR的理解和配置
- Andengine
- MySQL日期相关函数
- :SYSTEM.BLOCK_STATUS 控制BLOCK的插入操作
- yum源配置