Sum of integers

Source: Internet
Author: User

Problem Description:

Calculate the sum of the integers a and b, but you is not allowed to use the operator an + D - .

Example:
Given a = 1 and b = 2, return 3.

Problem Analysis:

First, we can analyze how people do decimal additions, 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 the sum do not carry, the result of the addition is 12 (single digit 5 and 7 add do not carry is 2, 10 digits 0 and 1 The result is 1); The second step is carry, carry in 5+7, the value of carry is 10; The result of 10 is 22, just 5+17=22.
In front of us is thinking, beg two number of and arithmetic can not use, that can also use what AH? Yes, what else can I use? In addition to the arithmetic, there are only bit operations left in the calculation of numbers. Bit operation is for binary, we also take the binary to analyze the previous three-step strategy on the binary is not also useful.
The binary of 5 is the binary of the 101,17 10001. Or try dividing the calculation into three steps: The first step is to add but not carry, the result is 10100 (the last two numbers are 1, the result of the addition is the binary 10.) This step does not count toward rounding, so the result is still 0); The second step is to write the carry. In this example, a carry is generated only when the last one is added, and the result is a binary 10; the third step adds the results from the first two steps, and the result is 10110, which is exactly 22. This shows that the three-step strategy is also useful for binary systems.
Next we try to replace the addition of the binary with the bitwise operation. The first step does not consider rounding, adding to each bit. The results of 0 plus 0 and 1 plus 1 0,0 plus 1 and 1 plus 0 are all 1. We can note 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, whereas 0 and 1, 1, and 0 have an XOR result of 1. Then consider the second carry, for 0 plus 0, 0 plus 1, 1 plus 0, will not produce a carry, only 1 plus 1 o'clock, will produce a carry forward. At this point we can imagine that two digits do the bit and the arithmetic first, then move one bit to the left. When only two numbers are 1, the bits and the results are 1, the rest are 0. The third step adds the results of the first two steps. If we define a function addwithoutarithmetic, the third step is equivalent to entering the results of the first two steps to call ourselves recursively.

Problem solving:

int getsum (intint  b) {       if(b = = 0)           {return A;       }        int num1 = a ^ b;        int num2 = (A & B) <<1;        return getsum (NUM1, num2);}

Sum of integers

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.