Integer operation (subtraction of the underlying operation)

Source: Internet
Author: User

For a computer, the subtraction method is all implemented with binary, so how do they do it?

First of all, the addition, there are two, in fact, the difference is not only the interpretation of different ways, the original code addition (unsigned) and complement addition (signed, add up is still the complement). Methods are the same, there is a drop of one, no drop zero, 21 carry. But this must consider the overflow problem, for the 32-bit machine, all the numbers are 32 bits to indicate, the addition may be moving one, that is, a maximum of 33 bits, what to do? 33 digits can not be expressed, in fact, is very simple, for the original code addition, as long as overflow, then the highest level must be one, the result will be the top bit of a forced truncation, so actually we see the value is (correct value -2^32) get. Then complement the addition is more troublesome, more than a sign bit, so you can discuss the situation: 1, negative minus, 2, positive and negative, 3, is positive, 4, negative negative. The first two types of overflows have no effect (it is easy to draw conclusions); The result shown after the third overflow is necessarily the complement of the correct value relative to the unsigned maximum number; the fourth case is summed up in its own way. =

Subtraction is not related to overflow, using addition to achieve, it is more troublesome to write, think about it.

Multiplication, first say overflow bar, multiplication overflow is very scary, in general, because the bit width is constant, beyond the width of the number to be truncated, and then show you, this shows the number and the correct value is related, display the number = correct value% positive maximum value (such as 32 bit 2147483648).  In addition to the question of multiplication efficiency, if we give them order, it must be this add subtraction multiplication division.  It is no exaggeration to say that multiplication is more than 10 times times less efficient than addition! What about that? There is no need to multiply it, so many compilers have been optimized, because by a number, always converted to the left and add subtraction combination operation, thus reducing the complexity, such as x*14, can be converted to (x<<3) + (X<<2) + (x<<1) can also be written ( X<<4)-(x<<1). Why is it?  Look at the binary code of 14 to know, 1110! Yes, the first form and the principle of fast power're far off. The second form can be used as an extension.

Division, Division is even slower, and in addition to not open rounding, negative numbers are rounded back (-3.14 rounding-4), positive to 0 rounding (3.14 rounding 3), oh yes, the compiler has evaded this non-habitual rounding method (by adding an offset to negative division), and there is no optimization method for division. There is no such rule as multiplication, so he is very slow, really slow, can not use it.

Integer operation (subtraction of the underlying operation)

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.