[Reprint] The nature and application of exclusive or

Source: Internet
Author: User

It is a binary-based bitwise operation that is expressed by the symbol XOR or ^.AlgorithmIt is the binary bit of the number on both sides of the operator. Take 0 for the same value and 1 for the same value. It differs from a Boolean operation in that when both sides of an operator are 1, the result of a Boolean operation is 1, and the result of an exclusive or operation is 0.

Simply put, it means not carrying addition, such as 1 + 1 = 0, 0 + 0 = 0 + 0 = 1.

Nature

1. Exchange Law

2. Combination law (I .e. (a ^ B) ^ c = a ^ (B ^ C ))

3. For any number x, x ^ x = 0, x ^ 0 = x

4,Inversion a xor B = a XOR 0 =

Exclusive or operations are most common in polynomial division, but their most important properties are self-inverse: a xor B = A, that is, for a given number A, the same calculation factor (B) is used) after two exclusive or operations, a itself is still obtained. This is a magical nature. With this nature, you can obtain many interesting applications. For example, allProgramTextbooks will point out to beginners that an intermediate variable must be introduced to exchange the values of two variables. However, if an exclusive or is used, the storage space of a variable can be saved: There are two variables A and B, and the storage values are A and B, respectively, the following three line expressions will swap their value expressions (values ):

A = a xor B (a xor B)

B = B xor a (B xor a xor B =)

A = a xor B (a xor B xor a = B)

Similarly, this operation can be applied in encryption, data transmission, verification, and many other fields.

Distance:

1-1001 is placed in an array containing elements, and only one unique element value is repeated.
Once. Each array element can only be accessed once. design an algorithm and find it.
Can we design an algorithm for implementation?

Solution 1. It is clear that someone has proposed a wonderful solution to add all the numbers, minus the sum of 1 + 2 +... + 1000.
This algorithm is perfect enough. I believe that the standard answer to the question is this algorithm. The only problem is that if the number of columns is too large, it may cause overflow.
Solution 2: this problem does not occur if it is different or, and the performance is better.
Returns an exclusive number or returns an exclusive number with 1 ^ 2 ^ 3 ^... ^ 1000.

However, although this algorithm is simple, it is not easy to prove. This is related to the features of exclusive or operations.
First, the exclusive or operation satisfies the exchange and combination laws.
So, 1 ^ 2 ^... ^ n ^... ^ n ^... ^ 1000, no matter where the two n appear, it can be converted to 1 ^ 2 ^... ^ 1000 ^ (N ^ N.

Second, for any number x, x ^ x = 0, x ^ 0 = x.
So1 ^ 2 ^... ^ n ^... ^ n ^... ^ 1000 = 1 ^ 2 ^... ^ 1000 ^ (N ^ n) = 1 ^ 2 ^... ^ 1000 ^ 0 = 1 ^ 2 ^... ^ 1000 (that is, all numbers except N in the sequence are exclusive or ).

1 ^ 2 ^... ^ 1000 (N is not included in the sequence) is t
Then the result of 1 ^ 2 ^... ^ 1000 (the sequence contains N) is t ^ n.
T ^ (t ^ n) = n.
Therefore, if all the numbers are exclusive or, the obtained result is different or from the result of 1 ^ 2 ^ 3 ^... ^ 1000, and the result is the number of duplicates.

Of course someone will say, 1 + 2 +... + 1000 of the results can be quickly calculated using the Gaussian law, but actually 1 ^ 2 ^... ^ 1000 results are also regular, and the algorithm is much simpler than Gauss's law. Deformation of Google interview questions: An array contains several integers, one number appears odd times, and the other appears even times. How can I find the number of odd times? There are a lot of solutions, but the best way is to compare all the numbers or, and the final structure is to be found. The principle is the same as above !!Reprinted from http://longzxr. I .sohu.com/blog/view/190676432.htmAn odd number is exclusive or exclusive. an even number is 0. 0 ^ A = A; an exclusive or exclusive law.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.