Without the aid of third switching variables a, b values of two variables

Speaking from a face questions ...

Without the aid of third switching variables a, b values ​​of two variables

A classic answer is to solve through XOR


a=a^b;
b=a^b;
a=a^b;

Keyword


[
    "异或",
    "逻辑运算符",
    "乔治·布尔",
    "香农"
]

prerequisites

0 is false and 1 is true

    插曲>>>如果读到"0为假,1为真",心底肯定略过了,这谁都知道的啊...
但是如果是写出来,但是找不到出处.即使常识也不太肯定了...

"^": Corresponding to the logical operation exclusive OR (XOR).
Focus on "different" and only [1] XOR [0] or [0] XOR [1] only 1; a true a fake that is true;

a ^ a = 0 ;
a ^ 0 = a ;

Prerequisites source and derivation

0 is false and 1 is true

Shannon already know, there is a mathematically logical algebra system, called Boolean logic, which is named after the British mathematician George Boole (George Boole). In Boolean logic, the results are not any logical expression of value, but "true", "false" both true value. You only need to use logical operators "and", "or," "not", you can express any logical statement that you want to express.

In logic, the logic value is true or a statement indicating to what extent is true. On the computer programmed multi-called Boolean value. In classical logic, the only possible true values ​​are true and false. But in other logic other true value is also possible.

A wave of forced supplement, if you have to want "a false, 0 is true", search:. "Negative logic"
on the origin of true and false, I can only find so much ...

a ^ a = 0 ;a ^ 0 = a ;

Binary arithmetic


    0110
^  0000
------------
=  0110

Then talk about exchange under the two variables.

In fact, although the variable value or [a, b]
but the value may appear in three.

For chestnut ↓
| a value of | B value | value occurs |
|: -: |: -: |: -: |
| 0 | 0 | [0] |
| 0 |. 1 | [0, 1] |
| 9 | 5 | [9,5,12] |
| ... | ... | ... |

The first step in a = a ^ b; In this case, a and b is equivalent to about binding relationship.
Only two variables, there is no third party interference, to swap the values of two variables, not binding relationship could not expect exchange it ...

Database point of view, it is similar to Tables A and B would have been no relations,
but want to exchange data between two tables, but also does not allow the emergence of the third table,
this time, to change the structure of the Table A Added one. (insert these words, I hope it will not let people understand more dizzy ...)

From the point of view of the relationship:

1. a relationship, at this time has changed a ...
The relationship is to assign a b, b ← a, 50 percent completed
according to the relationship, assigned to a b, a ← b .

Change the state from the point of view:

1.a evolved into a (state 1); B are retained;
2.b B evolved into a final state (state 1), namely: A;
3.a final state again evolved into a (state 2), namely: b ;

From the beginning with three variables, and then derive a formula to eliminate a variable point of view:

At this time a total of variable [a, b, c]

B = a ^ 1.c;
2.b = a;
// fact, prior to the third-step operation, a value that has not changed, the
// so c ^ a, can be deduced: (a ^ b) ^ a, this time to pay attention,
// this time b or c, b in, rather than step 2 has changed worth b. (this is not to change the reference value is not the type ... step 2 of the b , followed change the value of c)
// this is not a good place to be appreciated that the central idea: relationship between b and a first discharge pocket, the pocket is c, and by one kind of operation, and a value unchanged ,
// to counter the value of b is obtained;
3.a C ^ = a;

First Step 2 "b = a;", change it
as a ^ 0 = a; can be written as: b = a ^ 0;
as b ^ b = 0; can be written as: b = a ^ b ^ b ;
coupled with brackets;

B0 = A0 ^ 1.c0;
2.b1 = (a0 b0 ^) ^ B0;
3.a1 = (^ a0 b0) b1 ^;

In fact, this time, will be replaced by a c, is entirely possible,
because in addition to c after Step 1 is assigned, it no longer turn over value.
// A0 and a1 is a variable, but in order to distinguish a change in the value of the time, it will be written as a0 a1

B0 = A0 ^ 1.a1;
B0 = a1 ^ 2.b1;
3.a1 = a1 ^ b1;

A total of three assignment action, the first assignment in order to establish a relationship, and the remaining two, blatant exchange data.

Feel, a and b, in the process a total value of several changes.

Talking about feeling the last two steps still a little fuzzy, prawn hope to improve next ...

GG ... good night ...

Spread

  • 逻辑运算["~","&","|","^"]

Edit time list

. [1] November 15, 2016 18:17:41
[2] One II II 2010 - January fifteenth night

Guess you like

Origin www.cnblogs.com/love-zf/p/12199326.html