Home > Articles > Programming

This chapter is from the book

This chapter is from the book

2–8 Sign Function

The sign, or signum, function is defined by

020equ03.jpg

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

021equ01.jpg

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

021equ02.jpg

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

Finally, we note that the formula 021fig01.jpg almost works; it fails only for x = –231.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.