Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

In the front-end of daily development, we rarely use xOR operations, but in some framework source code, will be useful to xor operations. When we read the source code, the code will not understand, today we will introduce xOR operations.

concept

Exclusive OR, exclusive OR, abbreviated eOR.

Xor (EOR) is a mathematical operator. It applies to logical operations. The mathematical symbol of xOR is ⊕, and the computer symbol is Eor. The algorithm is as follows:

Operation rule

Equal to 0, different to 1

For example, there are two binary numbers, a and b, which are xOR computed.

An xor at 0 is this number

a ^ 0 = a
Copy the code

Any number that is either or to itself is equal to 0

a ^ a = 0
Copy the code

It satisfies the commutative and associative laws

The exchange of xor

a ^ b = b ^ a;
Copy the code

In combination with exclusive or

(a ^ b) ^ c = a ^ (b ^ c)
Copy the code

With these rules, we can use xOR in some practical scenarios

case

Interchange of two values (without using variables)

let a = 5; let b = 10; // switch a = a ^ b; b = a ^ b; a = a ^ b; console.log('a=>', a) console.log('b=>', b)Copy the code

Through the above code execution, we can see that the two numbers are swapped. Let’s briefly analyze the process

a = a ^ b; // Code executionCopy the code

A = a ^ b; b = 10;

b = a ^ b; // Code executionCopy the code

B = a ^ b ^ b.

B = a ^ 0 b = a ^ 0

And then we get b is equal to a.

a = a ^ b; // Code executionCopy the code

A = (a ^ b) ^ (a ^ b ^ b).

A = (a ^ a) ^ (b ^ b) ^ b

According to the law, then: A = b

Simple encryption and decryption

We have plain text, secret key, xOR operation to get cipher text

encryption

const cipher = text ^ key;
Copy the code

decryption

const text = cipher ^ key;
Copy the code

The principle is as follows:

const cipher = text ^ key;
const text = (text ^ key) ^ key;
Copy the code

Algorithm problem

If I have an array, and only one of them has an odd number of occurrences, and all the other trees have an even number of occurrences, how do I find the number that has an odd number of occurrences?

Ex. :

Const a =,3,4,5,6,5,5,6,4,3,2 [2]; // Print odd number = 5;Copy the code

We do xor, and the code is as follows

Const arr =,3,4,5,6,5,5,6,4,3,2 [2]; let number = arr[0]; for (var i = 1; i < arr.length; i ++) { number = number ^ arr[i] } console.log('number=>', number);Copy the code

The principle of

We xor every number in the array, and we get the odd number of occurrences. Assuming that different numbers are expressed in terms of variables, the following formula is obtained

a = 2; b = 3; c = 4; d = 5; e = 6; // A ^ b ^ c ^ d ^ e ^ d ^ d ^ e ^ c ^ b ^ a; A ^ a ^ b ^ b ^c ^c ^ d ^ d ^ e ^ e ^ dCopy the code

conclusion

So that’s xOR, but we’re not going to be scared when we look at these kinds of problems.

If you think the article is good, fine

1, like, so that more people can see this content (collection does not like, are playing rogue -_-)

Focus on me, let’s become a long-term relationship

3, pay attention to the public number “front have words”, there have been a number of original articles, and development tools, welcome your attention, the first time to read my article