The idea of reading a question from the programmer's interview guide

Source: Internet
Author: User

Today I readProgramA question in the employee Interview Guide

1 IntF (IntX,IntY)2 {3Return(X & Y) + (x ^ y)>1);4}

This question is equivalent to adding two numbers by 2.

I learned the truth after analyzing it for a long time.

X & Y is equivalent to the same bit in the binary number of X and Y. X ^ y is equivalent to taking different bits in the binary numbers of X and Y.

The sum of the same parts of x and y divided by 2 is X & Y. The result is that the same binary bit is 0 or 1, and the different parts must be 0.

The difference is definitely a 0, a 1, so after the difference or, when the sum of 0 and 1 does not produce carry, and the same part must be 0.

Then add the two together, which is X and Y, and divide them by 2.

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.