Witscale Test Center

9.4 Binary operators > 9.4.2 Modulo operator %


9.4.2 Modulo operator %

The modulo operator % has higher precedence among the binary operators. Its precedence is same as the * and / operators. You may not be familiar with this operator, as modulo operation is usually not a part of basic arithmetic we learn in school. When applied to two integers, this operator gives the remainder of division of the first operand by the second operand. For instance, 8 % 3 is the remainder of division operation of 8 by 3, which is 2. Since division and remainder is relevant only in case of numbers, the modulo operator is applicable only to the numeric types.

The behavior of modulo operator is not very straightforward when it comes to the negative or floating-point numbers. The basic approach to find a remainder is to divide. But if division seems complicated to you then you can use alternative approach. You need to keep on subtracting the second operator from the first one until the first operator becomes smaller than second. This value is the remainder.

 

One of the basic rules of computing is that you can achieve multiplication by repetitive addition and division by repetitive subtraction.

 

For instance, in the expression 6.5 % 1.2, you can find out the remainder by repetitive subtraction.  In the following manual steps, the second operand is repetitively subtracted from the first operand to find the remainder of the division.

 

1.       6.5 -1.2= 5.3    ==========è  5.3 is not smaller than 1.2 , continue …

2.       5.3 -1.2= 4.1    ==========è  4.1 is not smaller than 1.2 , continue …

3.       4.1 -1.2= 2.9    ==========è  2.9 is not smaller than 1.2 , continue …

4.       2.9 - 1.2= 1.7   ==========è  1.7 is not smaller than 1.2, continue …

5.       1.7 -1.2= 0.5    ==========è Now the result is smaller than 1.2, hence it is the remainder.

 

Alternatively, you can multiply both the operands by powers of 10 convert them both into the whole numbers. You may then do the division and find the reminder. Divide this remainder by the same powers of 10. For example, to evaluate 6.5 % 1.2 , you can follow these steps:

 

1.       Convert operands into whole numbers by multiplying both 6.5 and 1.2 by 101 .

2.       Find 65 % 12 which is 5.

3.       Divide 5 by 101. Therefore remainder of 6.5 and 1.2 is 0.5

 

The modulo operation for negative numbers may appear complex. But it is much simpler if you apply a rule of thumb. Simply forget about the sign of both the operands and perform the regular modulo operation. Then apply the sign of the first operand to the result to get the remainder. The following are some examples for performing modulo operation on signed operands.

 

  int a = 8 % 3;           // Value of a is 2

  int a = -8 % 3;          // Value of a is -2

  int a = 8 % -3;          // Value of a is 2

  int a = -8 % -3;         // Value of a is -2

 

Note that both expressions -8 % 3 and -8 % -3 evaluate to -2. Thus the sign of modulo expression depends only on first operand, the sign of second operand is irrelevant.