Title: Write a function that asks for the sum of two integers, requiring no use of +,-, x, and/or in the body of the function.
Analysis: This is another interesting topic to examine divergent thinking. The key to solving this problem is how to break through conventional thinking when something we are accustomed to is limited.
See this topic, my first reaction is dumbfounded, arithmetic can not use, that also what ah? But the problem is always to be solved, can only open thinking to think of various possibilities. First we can analyze how people do decimal additions, such as how to get the result of 5+17=22. In fact, we can be divided into three steps: The first step is to do the sum not carry, at this time the result of the addition is 12 (Single-digit 5 and 7 addition do not carry is 2, 10 digits 0 and 1 Add the result is 1); The second step carries the carry, the 5+7, the value of the carry is 10, and the third step adds up the previous two results, 12+ The 10 result is 22, just 5+17=22.
In front of us to think, two number and arithmetic can not be used, that can also use what AH? Yes, what else can you use? On the number of operations, in addition to arithmetic, there is only a bit of operation. Bit operation is for binary, we also use the binary system to analyze the previous three-step strategy to the binary is not also useful.
The binary of 5 is the binary system of 101,17 10001. Or try to divide the calculation into three steps: The first step you add but do not count, the result is 10100 (the last two are 1, the result is binary 10.) This step does not count, so the result is still 0), and the second steps down the carry. In this example, only a carry is produced when the last one is added, and the result is 10 of the binary; The third step adds the result of the first two steps, and the result is 10110, exactly 22. This shows that the three-step strategy is also useful for the binary system.
Next we try to replace the binary with a bitwise operation. The first step is to add to each bit without regard to the rounding. 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 result of the difference. For XOR or in terms of 0 and 0, 1 and 1, the result is 0, and the differences or results of 0 and 1, 1 and 0 are 1. Then consider the second step, and for 0 plus 0, 0 plus 1, 1 plus 0, there will be no rounding, only 1 plus 1 o'clock, which will produce a carry forward. At this point, we can imagine that the two numbers first do the bit and the operation, and then move one bit to the left. When only two numbers are 1, the bit and the result is 1, and the rest is 0. The third step adds the results of the first two steps. If we define a function addwithoutarithmetic, the third step is equivalent to the result of entering the first two steps to recursively call itself.
With these analyses, it is not difficult to write the following code:
Copy Code code as follows:
int addwithoutarithmetic (int num1, int num2)
{
if (num2 = 0)
return NUM1;
int sum = num1 ^ num2; XOR or operation
int carry = (NUM1 & num2) << 1; For 0 plus 0, 0 plus 1, 1 plus 0, no rounding is generated, only 1 plus 1 o'clock, and a carry is produced forward. So two numbers first do the bit and the operation, then move one bit to the left.
Return addwithoutarithmetic (sum, carry);
}