How Does Modulus Divison Work
I don't really understand how modulus division works.
I was calculating 27 % 16
and wound up with 11
and I don't understand why.
I can't seem to find an explanation in layman's terms online. Can someone elaborate on a very high level as to what's going on here?
Solution 1:
Most explanations miss one important step, let's fill the gap using another example.
Given the following:
Dividend: 16
Divisor: 6
The modulus function looks like this:
16 % 6 = 4
Let's determine why this is.
First, perform integer division, which is similar to normal division, except any fractional number (a.k.a. remainder) is discarded:
16 / 6 = 2
Then, multiply the result of the above division (2
) with our divisor (6
):
2 * 6 = 12
Finally, subtract the result of the above multiplication (12
) from our dividend (16
):
16 - 12 = 4
The result of this subtraction, 4
, the remainder, is the same result of our modulus above!
Solution 2:
The result of a modulo division is the remainder of an integer division of the given numbers.
That means:
27 / 16 = 1, remainder 11
=> 27 mod 16 = 11
Other examples:
30 / 3 = 10, remainder 0
=> 30 mod 3 = 0
35 / 3 = 11, remainder 2
=> 35 mod 3 = 2