Addition without the +,-, ×, and limit numeric Operators

Source: Internet
Author: User

Address: http://zhedahht.blog.163.com/blog/static/254111742011125100605/

The first response to this question is bitwise computation, but I can't figure out how to perform bitwise computation. I can only see it later. The example given by the blogger is 5 + 17.

Do not carry, calculate 5 + 17 = 12, for example, is the calculation of primary school students, the following 1 is carry

In this way, the last 12 + 10 digits of 1 are 22.

Similarly, binary can also be used. The binary value of 5 is 10001, and the value of 17 is. The calculation method is as follows:

00101

+ 10001

------

10100

+ 10

------

10110

The final calculation is 10110, Which is 22.

It can also be seen from this that in the binary, 1 + 1 and 0 + 0, in-situ 0 + 0 in-situ is 1, and is similar to or

What about carry,Pair0Add0,0Add1,1Add0Not generate carry, only1Add1A carry forward is generated, and the carry is equivalent to <1, so you can start writingProgramNow

 1   Int Addwithoutarithmetic (  Int  Num1,  Int  Num2)
2 {
3 If (Num2 = 0 )
4 Return Num1;
5
6 Int Sum = Num1 ^ Num2;
7 Int Carry = (Num1 & Num2) < 1 ;
8
9 Return Addwithoutarithmetic (sum, carry );
10 }

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.