# The implementation and negative-mode of division-C + +

Source: Internet
Author: User
Tags integer division

One, the following questions you can do all right?
1.7/4=?
2.7/(-4) =?
3.7%4=?
4.7% (-4) =?
5. (-7)/4=?
6. (-7)%4=?
7. ( -7)/(unsigned) 4 =?
1
-1
3
3
-1
-3
1073741822

If you get it all right, you can ignore what's behind it ...

Second, the division of the Rounding classification
Division is divided into three categories: rounding up, rounding down, to 0 rounding.
1. Rounding up: An integer that is closest to the exact value to the +∞ direction. In this rounding mode, 7/4=2,7/(-4) =-1,6/3=2,6/(-3) =-2
2. Rounding down: An integer that is closest to the exact value to the-∞ direction. In this rounding mode, 7/4=1,7/(-4) =-2,6/3=2,6/(-3) =-2
3. Rounding to 0: Take the integer nearest to the exact value in the 0 direction, in other words, the fractional part is rounded off, so it is called truncation rounding. In this rounding mode, 7/4=1,7/(-4) =-1,6/3=2,6/(-3) =-2
As you can see, whether it is rounding up or down, (-a)/b==-(A/b) is not necessarily true. This brings great trouble to the programmer. And for the 0 rounding, (-a)/b==-(A/b) is established, in this way, C + + uses this method of rounding.

Three, negative number to take the model
Recall the formula of primary school: Dividend ÷ divisor = quotient ... Remainder.
So, remainder = divisor-quotient x divisor (*)
For C + +, the (*) formula is still established. Moreover, this formula is the key to solving the problem of negative modulus.
Example one: 7% (-4) =?
Solution: The method of dividing the integer into 0 is divided into 7/(-4) =-1; by (*) type, remainder =7-(-4) * (-1) =3. So, 7% (-4) =3
Example two: (-7)%4=?
Solution: The method of dividing the integer into 0 is known as (-7)/4=-1; by (*) type, remainder = (-7) -4* (-1) =-3. So, (-7)%4=-3
Example three: (-7)% (-4) =?
Solution: The method of dividing the integer into 0 is divided into the following ways, (-7)/(-4) = 1; by (*), remainder = (-7)-(-4) *1=-3. So, (-7)% (-4) =-3

Iv. Development of relevant knowledge
1. For the division between signed integers and unsigned integers, C + + converts signed integers to unsigned integers, with special attention to the fact that the sign bit is not lost, but instead becomes a data bit participation operation. This is why ( -7)/(unsigned) 4 is not equal to-1, and equals 1073741822.

Original here: Why is it here? The number of two different sign bits is to be converted to the number of the same symbol bit, there is a rule, if the number of unsigned is greater than or equal to the number of symbols, then the number of signed bits is converted to unsigned, if the number of unsigned is less than the number of signed, then it depends on the compiler. Assuming that the int is 32 bits, then the largest can be expressed is 4294967296, at this time, with 7 to this number modulo, get 4294967289, this is the number of unsigned bits obtained after conversion, and then use this number in addition to 4, is 1073741822, the remainder is 1

2. Compiler optimization of division
① in "no optimization" conditions, the compiler will not affect the normal debugging of the premise, the division is simple optimization.
A. " Constant/constant Type division: The compiler calculates the result directly.
B. " Variable/variable "type Division: no optimization.
C. " Variable/constant "type division: if the constant ≠2^n, no optimization; otherwise, the Division will be converted to the right shift operation. Since the integer division implemented by the right-shift operation is essentially a downward rounding, the compiler will convert the downward rounding to 0 rounding with some additional instructions without generating a branching structure.

Taking "variable/2^3" as an example, the disassembly code is as follows:
mov eax, dividend
CDQ; if eax<0, then edx=0xffffffff; otherwise edx=0
and edx,7; if eax<0, then edx=7; otherwise edx=0
Add Eax,edx; If eax<0, the value of "(eax+7)/(2^3)" Rounding down is equal to the value of "eax/(2^3)" to 0, thus achieving rounding to 0
SAR eax,3; Move right to complete division
② in the "O2 optimization" condition, "variable/constant" type division, constants if ≠2^n, can also be optimized. At this point, the division is converted to a combination of multiplication and right shift. For example, a/b=a* (1/b) =a* ((2^n)/b) * (1/(2^n)), where ((2^n)/b is magicnumber, calculated by the compiler during compilation. So A/b becomes the (A*magicnumber) value of the >>n,n is selected by the compiler. It is important to note that this formula is only a typical representation in division optimization, and the compiler adjusts the formula based on the divisor, but the basic form is similar to the principle.

The implementation and negative-mode of division-C + +

Related Keywords:
Related Article

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.