Two number of different or operational exchanges

Source: Internet
Author: User
A = A^b;
b = a^b;

This allows a two-digit exchange to be achieved without the help of a third variable.

Based on the following properties: Arbitrary variable x and its own XOR or result is 0, that is: x^x = 0 arbitrary variable x and 0 to be different or operation, the result is unchanged, namely: X^0 = x xor or operation of the binding: A^b^c = (a^b) ^c = a^ (b^c) The exchange of the XOR: A^b = b^a

Explain why the above exchange works:

A = A^b
b = a^b--> B = (a^b) ^b = a^ (b^b) = A^0 = A, completes the process of A's value to B.
A = a^b--> a = (a^b) ^a = (b^a) ^a = b^ (a^a) = B^0 = B completes the process of B's value to a.

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.