2-7. Sign Function

The sign, or signum, function is defined by

It may be calculated with four instructions on most machines [Hop]:

If you don’t have shift right signed, then use the substitute noted at the end of Section 2-6, giving the following nicely symmetric formula (five instructions):

Comparison predicate instructions permit a three-instruction solution, with either

Equation 3

Finally, we note that the formula almost works; it fails ...

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.