[C language] XOR (^) operator

1. Introduction

Exclusive OR, English is exclusive OR, abbreviated as xor

Exclusive OR (xor) is a mathematical operator. It is used in logical operations. The mathematical symbol for XOR is "⊕" and the computer symbol is "xor". Its algorithm is:

a⊕b = (¬a ∧ b) ∨ (a ∧¬b)

If the two values ​​​​a and b are not the same, the XOR result is 1. If the values ​​of a and b are the same, the XOR result is 0.

XOR is also called half addition operation. Its operation rule is equivalent to binary addition without carry: in binary, 1 is used to represent true and 0 is false. Then the operation rule of XOR is: 0⊕0=0, 1⊕0= 1, 0⊕1=1, 1⊕1=0 (the same is 0, the difference is 1), these rules are the same as addition, but without carry, so XOR is often regarded as addition without carry.

Exclusive OR is abbreviated as XOR, EX-OR

There are two operators in the program: XOR and ⊕.

How to use it:

z = x ⊕ y

z = x xor y


2. Algorithm

  1. Return to zero law: a⊕a=0
  2. Identity law: a⊕0=a
  3. Commutative law: a⊕b=b⊕a
  4. Associative law: a⊕b⊕c=a⊕(b⊕c)=(a⊕b)⊕c
  5. Reflexive: a⊕b⊕a=b
  6. d=a⊕b⊕c can deduce a=d⊕b⊕c
  7. If x is a binary number 0101 and y is a 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, "when the two inputs are the same, they are 0, and if they are different Then it is 1".
    a b a⊕b
    0 0 0
    0 1 1
    1 0 1
    1 1 0

3. Logic

Logical expression : F = AB' + A'B

The truth table of XOR logic is shown in Figure 1:

picture

Its logical symbol is shown in Figure 2:

figure 2

The relationship of XOR logic is: when AB is different, the output P=1; when AB is the same, the output P=0. "⊕" is the XOR operator symbol. XOR logic is also a combination of AND or non-logic. Its logical expression is:

P=A⊕B

As can be seen from Figure 1 , the rules of XOR operation are:

0⊕0=0,0⊕1=1

1⊕0=1,1⊕1=0

Tip 1:

If they are the same, take 0; if they are different, take 1.

Tip 2:

If input A is 0, then output p = input B

If input A is 1, then output p = the inverse of input B

In fact, the definition of XOR in English is either one (is one), but not both, that is, when only one is true (1), take true (1).


4. Function

Commonly used in computers, the logical symbol for exclusive OR (xor) is generally xor, but ⊕ is also useful:

true⊕false=true

False⊕True=True

false⊕false=false

true⊕true=false

Or for:

True ⊕ False = True

False ⊕ True = True

False⊕ False = False

True ⊕ True = False

Some computer languages ​​use 1 to represent true and 0 to represent false, so the bitwise XOR of the two bytes is as follows

00000000

xor

00000000

-------------

result

00000000

The following is an XOR calculation of two binary values:

11111111

xor

00000000

--------------

result

11111111

In reality, all decimal values ​​are used, so let’s take a look at how two decimal values ​​are XORed:

5 ⊕ 3 = ?

1. All values ​​will be converted to binary before XOR calculation:

5 and 3 converted to binary are: 0101, 0011 respectively

0101

xor

0011

--------

result

0110

2. Convert the result 0110 to decimal: 6

3.So 5 ⊕ 3 = 6


5. Clever use

Different from other languages, C language and C++ language (C++ supports xor, the usage and effect are the same as '^') do not use xor, but use "^", and the typing method is Shift+6. (The "^" in other languages ​​generally means power)

If you need to exchange the values ​​of two variables, in addition to the commonly used intermediate variables for exchange, 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
//此时a1=a^b  b=a
a=a1^a=a^b^a=b

Notice:

a=a^b^(b=a);//此类形式是不正确的UB行为,在不同编译器中会有不同的结果,切勿使用

This completes the exchange of a and b.

To sum up: the same variable is XORed with another variable and its XOR value is equal to another number, such as (a^b)^b=a.

Use case: It can be used in one or more aspects of the encryption algorithm to make the algorithm more complex, difficult to crack, and more secure.


6. Relationship with addition and subtraction operations

The addition and subtraction operations on the Galois field are equivalent, that is, the XOR operation. Multiplication and division directly multiply and divide polynomials and then take the modulo of the primitive polynomials.

Guess you like

Origin blog.csdn.net/weixin_72357342/article/details/132778244