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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.