[Algorithm 26] Do not use +,-, ×,/for addition.

Source: Internet
Author: User

[Topic] Write a function and calculate the sum of two integers. +,-, ×,/Are not required in the function body ,/.

[Si Lu] Everyone who has learned digital power knows that +,-, ×, And/are basically full processors used in computer processing, while the full-processors used a lot of logic door circuits for computing (I cannot remember these door circuits now ), therefore, the basis of the operation is actually the combination of binary logic operations. Since the question does not require us to use +,-, ×,/, we can start directly from the underlying logical operation.

For the sake of convenience, we may take 4 + 14 as an example. The binary value of 4 is 1110, And the binary value of 14 is. I remember there is a register used to store carry in power, that is to say, we first calculate the sum of all without carry, and then add the number of each bit from the next bit to get the result. Specifically: First, we calculate the do not carry form of 100 + 1110 as: 1010, and then calculate the carry number as: 1000, plus 10010 = 18, which is completely correct.

What bitwise operations are used to implement the above operations? First, addition does not carry, subtraction does not return, which is an expression of the "exclusive or" operation. Therefore, we first calculate a ^ B. Second, we calculate the number produced by carry. For binary, only 1 + 1 generates carry (1 + + + 0 + 0 do not generate carry), that is, when and only when 1 + 1 appears, we should set the output value to 10. We can use "bitwise AND". In this way, when appears at the same time, the result is 1 and the rest are 0, but we want to get 10. What should we do? We can move one to the left. In this way, the second problem is also solved. The third problem is to add the results of the first two results and then call them recursively.

Based on this idea, we can write the following:Code:

 1 # Include <iostream>
2 # Include <String >
3 Using Namespace STD;
4
5 /* **************************************** *****
6 * Add without use + -*/
7 **************************************** ***** */
8 Int Pluswithoutarithmetic ( Int A, Int B)
9 {
10 If (B = 0 )
11 Return A;
12
13 Int Sum = a ^ B;
14 Int Temp = (A & B) < 1 ;
15
16 Return Pluswithoutarithmetic (sum, temp );
17 }
18
19 Int Main ()
20 {
21 Cout < " Enter the two numbers: " <Endl;
22 Int Num1 = 0 ;
23 Int Num2 = 0 ;
24 Cin> num1> num2;
25
26 Cout < " The plus result is: " <Endl;
27 Cout <pluswithoutarithmetic (num1, num2) <Endl;
28
29 Return 0 ;
30 }

The test results are as follows:

References:

ProgramMember interview questions featured 100 questions: http://zhedahht.blog.163.com/blog/static/254111742011125100605/

Note:

1) Compile all the code environments in this blogWin7 + vc6. All code has been debugged by the blogger.

2) BloggerPython27This blogArticleCopyright. For Network reprinting, please indicate the sourceHttp://www.cnblogs.com/python27/. You have any suggestions for solving the problem. Please feel free to comment on them.


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.