The sword refers to the offer surface question (Java Edition): Do not add subtraction

Source: Internet
Author: User

Title: Write a function, the sum of two integers, required in the function body should not be applicable +,-,*,./Arithmetic symbols

The interview was asked this question, first of all, we analyze how people do decimal addition, such as how to get the results of 5+17=22, in fact, we can be divided into three steps: The first step is only to do you add not carry, at this time the result is 12, the second step to do carry, 5+7 with carry, The value of the carry is 10; the third step, add up the previous two results 12+10 the result is 22, just 5+17=22

We have been thinking, ask two trees and arithmetic can not be used, but also what? In addition to the arithmetic, there are only bit operations left in the calculation of numbers. Bit operation is for binary, and we will analyze the previous three-step strategy for binary system is also applicable.

The binary of 5 is the binary of the 101,17 is 10001, or try to divide the calculation into three steps: The first step is summed up but not counted, the result is 10100; the second step is to write the carry. In this example, only the last one to add a carry, the result is a binary 10, the third step to add the results of the first two steps, the result is 10110, the conversion to decimal is just 22, this shows that the three-step strategy for the binary is also applicable.

Next we will try to replace the binary addition with the bitwise operation. The first step does not consider the rounding to add to each bit. The result of 0+0,1+1 is that the result of 0,1+0 is 1. The result of 0+1 is 1, and we notice that this is the same as the outcome of the XOR. For XOR, the result of 0 and 0,1 and 1 XOR is 0, and the result of 0 and I1 and 0 xor me, then consider the second step carry, for 0+0,0+1,1+0, will not produce a carry, only when the next, the poor forward a carry. At this point we can imagine that two digits are first done and then moved forward to the left. Only two numbers are at 1. The third step is to repeat the previous two steps until no rounding is generated.

After figuring out the process, write a very brief introduction to the code. As follows:

/** * Do not subtraction add */package swordforoffer;/** * @author Jinshuangqi * * August 11, 2015 */public class E47addtwonumbers {public I NT Add (int num1,int num2) {int sum,carry;do{sum = num1 ^ Num2;carry = (num1&num2) <<1;num1 = sum;num2 = Carry;} while (num2!=0); return NUM1;} public static void Main (string[] args) {e47addtwonumbers test = new E47addtwonumbers (); System.out.println (Test.add (5, 17));}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The sword refers to the offer surface question (Java Edition): Do not add subtraction

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.