Add two numbers without four arithmetic operations

Source: Internet
Author: User

Analysis: there are no four arithmetic operations for adding two values. If there are no four arithmetic operations in the computer, it must be a bit operation.

(The following analysis is from offoffoffoffer) for example, we calculate the result of 5 + 17 = 22. In the world, we can divide it into three steps. In the first step, the numbers are not carried together, at this time, the result is 12 (the single-digit addition does not carry 2, and the ten-digit addition is 1), so the result is 12; the second step is carry, 5 + 7 has a carry-in, and the carry value is 10; the third step is to add the results of the previous two steps. 12 + 10 = 22.

So we can also consider using bitwise to calculate binary numbers. 5 is binary 10001 and 17 is binary. Try to divide the computation into three steps. The first step is to add the numbers without carry. The result is 10100. The second step is to write down the carry. Based on this example, the carry computation result is 10; the third step is to add the results of the previous two steps to get the result 10110. it is converted to decimal, Which is 22. In binary calculation, the first step does not consider carry. That is, each bit is added with 0 + 0 = + 1 = + 1 = + 0 = 1. The result is an exclusive or operation of binary data. In the second step, only the carry operation is considered. 0 + + + 0 will not carry the place. Only 1 + 1 will carry the place. The result conforms to the two numbers and the operation result is then shifted to the left. Step 3: add the results of the previous two steps. The addition step is to repeat the previous two steps. Until the carry value is not generated.

// Implement the addition operation without having to use four numbers # include <stdio. h> # include <stdlib. h> int add (INT num1, int num2) {int sum = 0, carry = 0; do {sum = num1 ^ num2; carry = (num1 & num2) <1; num1 = sum; num2 = carry;} while (num2! = 0); Return num1;} int main () {int num1, num2; printf ("enter two integers: \ n"); scanf ("% d ", & num1, & num2); int res = add (num1, num2); printf ("Result: % d", Res); Return 0 ;}

 

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.