How to Implement addition by using the +,-, ×, and limit numeric operators in C language?

Source: Internet
Author: User

How to Implement addition by using the +,-, ×, and limit numeric operators in C language?
Question: To write a function and calculate the sum of two integers, you must not use +,-, ×, & pide; In the function body ;.

Analysis: This is a very interesting question to study divergent thinking. When something we get used to is restricted, how to break through the conventional thinking is the key to solving this problem.

My first response to this question is dumb. I cannot use any of the four arithmetic operations. What else can I use? But the problem is always to be solved. You can only start thinking about various possibilities. First, we can analyze how people perform decimal addition, for example, how to obtain the result of 5 + 17 = 22. In fact, we can divide it into three steps: the first step is to add each other without carrying, and the result of adding is 12 (the number of digits 5 and 7 is not 2, the result of adding 10 digits 0 and 1 is 1). In Step 2, carry is performed. In Step 5 + 7, carry is 10. in step 3, add the first two results, the result of 12 + 10 is 22, that is, 5 + 17 = 22.

We were just wondering what else can be used to calculate the sum of two numbers and four arithmetic operations? Right. What else can I use? In addition to the four arithmetic operations, only bitwise operations are performed on numbers. Bitwise operations are for binary, so we can use binary to analyze whether the preceding three-step strategy also works for binary.

The binary value of 5 is 10001. We still try to divide the calculation into three steps: the first step is to add but not to carry the result. The result is 10100 (the last two digits are 1, and the result of the addition is 10 of binary. This step does not include carry, so the result is still 0); the Second Step writes down the carry. In this example, only a carry is generated when the last bit is added, and the result is 10 of the binary. In the third step, the result of the first two steps is added, and the result is 10110, exactly 22. It can be seen that the three-step strategy is also useful for binary.

Next, we try to replace the addition in binary with bitwise operations. The first step is to add each bit without considering carry. The results of 0 plus 0 and 1 plus 1 are both 0, 0 plus 1 and 1 plus 0. We can note that this is the same as or. For an exclusive or, 0 and 0, 1 and 1 exclusive or result is 0, while 0 and 1, 1 and 0 exclusive or result is 1. Next, consider the second step. For 0 plus 0, 0 plus 1, 1 plus 0, there will be no carry. Only when 1 plus 1, there will be a carry forward. In this case, we can think of two numbers first performing bitwise AND operations, and then moving one digit to the left. When the two numbers are both 1, the result of bitwise AND is 1, and the rest are 0. Step 3: add 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.

With these analyses, it is not difficult to write the following code:

Int AddWithoutArithmetic (int num1, int num2) {if (num2 = 0) return num1; int sum = num1 ^ num2; // int carry = (num1 & num2) <1; // For 0 plus 0, 0 plus 1, and 1 plus 0, do not generate carry. When only 1 plus 1, a carry is generated forward. Therefore, the two numbers first perform bitwise AND operation, and then move one digit to the left. Return AddWithoutArithmetic (sum, carry );}

 

Related Article

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.