Application of XOR operation

Source: Internet
Author: User

A binary-based bitwise operation, denoted by the symbol XOR or ^,

The algorithm is for each bits of the operator's sides, the same value takes 0, and the value is 1.

It differs from Boolean operations in that when both sides of the operator are 1 o'clock, the result of the Boolean operation is 1, and the result of the XOR operation is 0.

The nature of a different or
    1. Exchange Law: a ^ b = b ^ a
    2. Binding law: a ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ C
    3. D = a ^ b ^ C can be introduced a = d ^ b ^ C
    4. Reflexivity: a ^ b ^ a = b
    5. x ^ x = 0, x ^ 0 = x
Application: 1. The first thing we usually do is swap two numbers:
void Exchange (intint  b)  {      ^= b;       ^= A;       ^= b;  }  

The reflexive nature of the XOR operation is used here:

First A = a ^ b;

Then B = a ^ b ^ b = a

Then A = a ^ b ^ a = b

1-1000 is placed in an array of 1001 elements, and only one element value is duplicated, and the other appears only once. Each array element can only be accessed once, design an algorithm, find it out, can you design an algorithm implementation without secondary storage space?

Solution One : add up all the numbers, minus the 1+2+...+1000.

This algorithm is perfect enough to believe that the answer to the question is the algorithm, the only problem is that if the sequence is too large, it may lead to overflow.
Solution Two : XOR does not have this problem, and performance is better. All the numbers are different or, the results obtained are different from the results of the 1^2^3^...^1000, and the result is the number of repetitions.

Solution one obviously, the solution two need to prove that:

The preceding mentioned XOR has a commutative law and a binding law, so 1^2^...^n^...^n^...^1000, no matter where the two n appear, can be converted into the form of 1^2^...^1000^ (n^n). Second, for any number x, there are x^x=0,x^0=x. So 1^2^...^n^...^n^...^1000 = 1^2^...^1000^ (n^n) = 1^2^...^1000^0 = 1^2^...^1000 (that is, the XOR of all the numbers except n in the sequence). The 1^2^...^n^ of the order. ^1000 (contains one n in a sequence) results in t
Then 1^2^. ^n^. ^n^. The result of a ^1000 (2 n in a sequence) is t^n.
t^ (t^n) =n.
So, all the numbers are different or, the results obtained are different from the results of 1^2^3^...^1000, and the result is a repetition number.

An array holds a number of integers, a number appears odd several times, the remaining numbers appear even several times, to find out the number of odd number of occurrences?

This is actually a problem of the deformation of the previous topic, the most direct approach or the same as above, is to put all the number of different or (odd number of different or itself, even the number is 0)

a 32-bit integer macro definition of any two bit exchange
#include <stdio.h>#defineBswap (data, m, n) \(Data& (1<< m) = = (Data & (1<< N))? Data:data ^ (1<< m) | (1<<N))voidMainvoid){    intdata =4; intm =2, n =3; //0000 0000 0000 0100//Exchange Bit2 and Bit3//0000 0000 0000//8printf ("%d\n, Bswap (2, 3),%d", data, Bswap (data, M, n));}

The clever thing about this writing is to use the XOR operation to make the so-called bit exchange, the main idea of the macro is that if the two bits are the same, it is natural to return the original data, if the two bits are different, then
is to change the original 0 to 1, and 1 to 0, then you can take the bit and 1 xor or you can reverse the

Application of XOR operation

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.