10-5. Signed Division by Divisors ≤ -2

Because signed integer division satisfies n ÷ (−d) = −(n ÷ d) it is adequate to generate code for n ÷ |d| and follow it with an instruction to negate the quotient. (This does not give the correct result for d = −2W − 1, but for this and other negative powers of 2, you can use the code in Section 10-1, “Signed Division by a Known Power of 2,” on page 155, followed by a negating instruction.) It will not do to negate the dividend, because of the possibility that it is the maximum negative number.

It is possible, however, to avoid the negating instruction. The scheme is to compute

Adding 1 if n > 0, however, ...

Get Hacker's Delight now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.