Division and Modulus

Division and Modulus

To ensure that the results of the "/" and "%" operators are defined correctly for signed arithmetic operations, both operands must be defined positive. (Refer to HC(S)08 Backend.) When one of the operands is defined negative, the implementation defines the sign of the result. See the following listing.

Listing: Effect of Polarity on Division and Modulus Arithmetic
#ifdef __MODULO_IS_POSITIV__
 22 / 7 ==  3;   22 %  7 == 1

 22 /-7 == -3;   22 % -7 == 1

-22 / 7 == -4;  -22 %  7 == 6

-22 /-7 ==  4;  -22 % -7 == 6

#else

 22 / 7 ==  3;   22 %  7 == +1

 22 /-7 == -3;   22 % -7 == +1

-22 / 7 == -3;  -22 %  7 == -1

-22 /-7 ==  3;  -22 % -7 == -1

#endif

The following table lists the modulus macro for the HC08 target.

Table 1. Modulus Macro Settings
Name Defined
__MODULO_IS_POSITIV__ Never