Some properties of XOR

Source: Internet
Author: User

http://artori.us/exclusive-or/

Do not introduce what is different or, someone called half Plus, the math department called the bitwise Modulus 2 plus

Some of the simple properties that are used below

    • x^0 = xAndx^x = 0

    • Exchange Law:x^y = y^x

    • Binding law:(x^y)^z = x^(y^z)

    • Reflexive nature:x^y^y = x

Here are a few small topics, you can use different or solve, very skillful

Exchange two number AB

Interesting is the search for other different or examples, found this article, the article implemented a different or exchange algorithm, and this article is not related to the topic, but it is interesting, the function more time should only manipulate the value rather than the variable.

update_2013-05-06: This, see Cloud Wind wrote a use this feature to do a doubly linked list, very fun, can not help writing over.

In a set, take the number x and get the B set, X.

Makes a XOR(X) representation that all the numbers in the X collection are different or

XOR(B)^XOR(A) = XOR(B)^XOR(B)^x = 0^x = x

via:http://www.javaeye.com/topic/420487

A set of x, y get B set, for X and Y

First, you can push the export by the previous methodxor(A)^xor(B) = xor(B)^xor(B)^x^y = 0^x^y = x^y

x^yBinary result, the nth bit is 1, the nth bit of x and Y is not the same

According to whether the nth bit is 0 all the numbers in a are divided into A1 and A0 two arrays (the binary nth bits of the number in the A1 are all 1,a0 0)

A1 and A0 should each contain a or B (so that nth bit can be different or 1)

Similarly, you can divide B into B1 and B0 two arrays

Can get the first numberx = A1^B1

The second number can y = A0^B0 , of course, be used to x^y^x obtain

In addition, if it x^y is 0, that is x == y , the sum of all the SUM(X) numbers within the X set

(SUM(A) - SUM(B)) / 2 = x

Via:http://blog.chinaunix.net/uid-12453618-id-2935334.html

In set a, only a few x appear 1 times, the remaining number is repeated 2 times, for X

xor(A) = x^y^y^…^z^z = x^(y^y^…^z^z) = x^0 = x

In set a, only a few x appear 1 times, the remaining number is repeated 3 times, for X

The nature of XOR is equivalent to "bitwise modulo 2 plus" (adding modulo 2), which makes P1,p2...pn a Boolean, true 1, False 0, and (+) represents an XOR operation.

p1 (+) p2 (+) ... (+) pn == ( p1 + p2 + ... + pn ) % 2

So we just need to implement the bitwise modulo 3 plus

( p1 + p2 + ... + pn ) % 3

Adds 0 or 1 of the same bits in the set that have a number of binary representations, and the final and the 3 to touch, resulting in X

A = {5, 7, 7, 7}For example, a binary represents two numbers

I hope this piece of stuff can make people understand.

Via:http://www.cs.umd.edu/class/sum2003/cmsc311/notes/bitop/xor.html

No ^Operating time to achieve XOR, use only &And |Or ~Non -

There's a lot of conversions here, like this one.

x ^ y == (~x & y) | (x & ~y)

See Wikipedia "equivalencies, elimination, and introduction" sections, various formulas

Some properties of XOR

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.