Mathematical symbol
Collection
zero Useful+1
zero
XOR, exclusive OR in English, abbreviation Xor
XOR is a mathematics operator It applies to Logical operation The mathematical symbol of XOR is "⊕", and the computer symbol is "xor". The algorithm is:
a⊕b = (¬a ∧ b) ∨ (a ∧¬b)
If the values of a and b are different, the XOR result is 1. If the values of a and b are the same, the XOR result is 0.
XOR is also called Banjia operation , its algorithm is equivalent to binary addition without carry: in binary, 1 represents true, 0 represents false, then the exclusive OR algorithm is: 0 ⊕ 0=0, 1 ⊕ 0=1, 0 ⊕ 1=1, 1 ⊕ 1=0 (the same is 0, the different is 1), these algorithms are the same as addition It is the same, but without carry, so XOR is often regarded as non carry addition.
XOR is abbreviated as XOR, EX-OR
There are two operators in the program: XOR and ⊕.
The use method is as follows
z = x ⊕ y
z = x xor y
Chinese name
XOR
Foreign name
exclusive OR
Mathematical symbol
English abbreviation
xor
Program symbol
^

Algorithm

Announce
edit
one Law of return to zero
two Law of identity
three Commutative law
four Associative law
;
five introspect
.
6. d=a ⊕ b ⊕ c can push a=d ⊕ b ⊕ c
7. If x is a binary number 0101, y is Binary number 1011;
Then x ⊕ y=1110
The result is 1 only when the two compared bits are different, otherwise the result is 0
That is, "0 if two inputs are the same, 1 if they are different"
a
b
a⊕b
zero
zero
zero
zero
one
one
one
zero
one
one
one
zero

logic

Announce
edit
Logical expression
The truth table of XOR logic is as follows Figure 1 As shown in.
chart
Its logic symbols are as follows Figure 2 As shown in.
The relationship of XOR logic is: when AB is different, output P=1; When AB is the same, output P=0. "⊕" is an exclusive OR operation symbol, and exclusive OR logic is also a combination of AND or non logic, and its logical expression is:
P=A⊕B
From Figure 1 It can be seen that the rule of XOR operation is
0⊕0=0,0⊕1=1
1⊕0=1,1⊕1=0
Figure 2
Formula 1: Take 0 for the same and 1 for the different
Formula 2:
Input A takes 0, then output p=input B
Input A takes 1, then output p=inverse of input B
In fact, XOR is defined as either one (is one), but not both in English, that is, when only one is true (1), the truth (1) is taken.

effect

Announce
edit
Widely used in computers, xor Logical symbol Generally, xor is also used:
True False=True
False True=True
False=False
True True=False
Or:
True ⊕ False = True
False ⊕ True = True
False⊕ False = False
True ⊕ True = False
Some computer languages use 1 to indicate true and 0 to indicate false, so the bitwise XOR of the two bytes is as follows
-
00000000
xor
00000000
-------------
result
00000000
The following two binary values are used for XOR calculation:
-
eleven million one hundred and eleven thousand one hundred and eleven
xor
00000000
--------------
result
eleven million one hundred and eleven thousand one hundred and eleven
In reality, all decimal values are used. Let's take a look at how two decimal values perform XOR calculation:
5 ⊕ 3 = ?
1. The values will be converted to binary before XOR calculation:
5 and 3 are converted to binary: 0101 and 0011 respectively
-
0101
xor
0011
--------
result
0110
2. Then convert the result 0110 to decimal: 6
3. So 5 ⊕ 3=6

Skillful use

Announce
edit
Unlike other languages, C language and C++Language (C++supports xor, whose usage and effect are the same as' ^ '.) Instead of xor, "^" is used, and the typing method is Shift+6. ("^" in other languages generally means power)
If it is necessary to exchange the values of two variables, in addition to borrowing intermediate variables, you can also use XOR to exchange only two variables, such as:
void swap(int &a,int &b) {     a=a^b;     b=b^a;     a=a^b; }
Detailed explanation:
a1=a^b b=b^a1=b^a^b=a //Now a1=a ^ b b=a a=a1^a=a^b^a=b
be careful:
a=a^b^(b=a);// This type of form is incorrect UB behavior, and different results can be obtained in different compilers. Do not use
This completes the exchange of a and b.
To sum up, the same variable and another variable and their exclusive OR value are exclusive or equal to another number, such as (a ^ b) ^ b=a.
Use case: It can be used in one or more links of the encryption algorithm, making the algorithm more complex, difficult to crack, and more secure.

Addition and subtraction relation

Announce
edit
Addition and subtraction operations on the Galois field are equivalent, that is, XOR operations. While multiplication and division are directly polynomial multiplication and division, and then Primitive polynomial Take the mold.