Note: The following code snippets only work when both operands are positive. I have omitted any handling of negative values because the results of x % y when one or both operands are negative vary between different languages and platforms. In general you can just calculate abs(x) % abs(y) and then perform some transformation on the result.
The simplest way to calculate x % y using only only addition and subtraction is to just repeatedly subtract y until the remaining value is less than y:
/* Compute x mod y naively. */
int mod_basic(int x, int y) {
int result = x;
while (result >= y)
result -= y;
return result;
}
However, this approach is fairly inefficient. A more complex but faster method is to use binary long division. This is similar to regular long division except in binary the result at each step is either 0 or 1 instead of 0..9. A basic approach to calculating x % y with BLD looks like this:
/* Compute x mod y using binary long division. */
int mod_bld(int x, int y)
{
int modulus = x, divisor = y;
while (divisor <= modulus && divisor <= INT_MAX/2)
divisor <<= 1;
while (modulus >= y) {
while (divisor > modulus)
divisor >>= 1;
modulus -= divisor;
}
return modulus;
}
One gotcha in the above: the divisor <= INT_MAX/2 is necessary to stop overflow in divisor <<= 1 when x > MAX_INT/2.
Additionally divmod, which gives you both the quotient and the modulus in one calculation, looks almost exactly the same:
/* Compute divmod(x, y) using binary long division. */
void divmod(int x, int y, int *div, int *mod) {
int quotient = 0, modulus = x, divisor = y;
while (divisor <= modulus && divisor <= INT_MAX/2)
divisor <<= 1;
while (modulus >= y) {
while (divisor > modulus) {
divisor >>= 1;
quotient <<= 1;
}
modulus -= divisor;
quotient++;
}
while (divisor != y) {
quotient <<= 1;
divisor >>= 1;
}
*div = quotient;
*mod = modulus;
}
Finally, note that if y is a power of 2, you can cheat. In this case x % y is just x & (y - 1).