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?
Robert Harvey♦
Get your hardware project on the right track. Whether you are in the initial stages of requirements definition or need extra resources or expertise to finish a project in the midst of development, immediate experienced assistance is only a mouse click away.
NSWOANSWOA
18 Answers
The result of a modulo division is the remainder of an integer division of the given numbers.
That means:
Other examples:
LeoLeo
Most explanations miss one important step, let's fill the gap using another example.
Given the following:
The modulus function looks like this:
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:
Then, multiply the result of the above division (
2
) with our divisor (6
):Finally, subtract the result of the above multiplication (
12
) from our dividend (16
):The result of this subtraction,
4
, the remainder, is the same result of our modulus above!Marcin M. JessaMarcin M. Jessa
Maybe the example with an clock could help you understand the modulo.
A familiar use of modular arithmetic is its use in the 12-hour clock, in which the day is divided into two 12 hour periods.
Lets say we have currently this time: 15:00
But you could also say it is 3 pm
But you could also say it is 3 pm
This is exactly what modulo does:
You find this example better explained on wikipedia: Wikipedia Modulo Article
PrinePrine
The simple formula for calculating modulus is :-
So, 27 % 16 :-
![Fpga How It Works Fpga How It Works](http://yosefk.com/img/n/fpga-clean.png)
27- {(27/16)*16}
27-{1*16}
Answer= 11
Note:
All calculations are with integers. In case of a decimal quotient, the part after the decimal is to be ignored/truncated.
eg: 27/16= 1.6875 is to be taken as just 1 in the above mentioned formula. 0.6875 is ignored.
Compilers of computer languages treat an integer with decimal part the same way (by truncating after the decimal) as well
Code_JamerCode_Jamer
The modulus operator takes a division statement and returns whatever is left over from that calculation, the 'remaining' data, so to speak, such as 13 / 5 = 2. Which means, there is 3 left over, or remaining from that calculation. Why? because 2 * 5 = 10. Thus, 13 - 10 = 3.
The modulus operator does all that calculation for you, 13 % 5 = 3.
RebelPhoenixRebelPhoenix
modulus division is simply this : divide two numbers and return the remainder only
27 / 16 = 1 with 11 left over, therefore 27 % 16 = 11
ditto 43 / 16 = 2 with 11 left over so 43 % 16 = 11 too
chrischris
Very simple:
a % b
is defined as the remainder of the division of a
by b
.See the wikipedia article for more examples.
Yuval AdamYuval Adam
I hope these simple steps will help:
20 / 3 = 6
; do not include the.6667
– just ignore it3 * 6 = 18
20 - 18 = 2
, which is the remainder of the modulo
mustafemustafe
I would like to add one more thing:
it's easy to calculate modulo when dividend is greater/larger than divisor
dividend = 5divisor = 3
5 % 3 = 2
but what if divisor is smaller than dividend
dividend = 3divisor = 5
3 % 5 = 3 ?? how
This is because, since 5 cannot divide 3 directly, modulo will be what dividend is
bn00dbn00d
Modulus division gives you the remainder of a division, rather than the quotient.
samozsamoz
Lets say you have 17 mod 6.
what total of 6 will get you the closest to 17, it will be 12 because if you go over 12 you will have 18 which is more that the question of 17 mod 6. You will then take 12 and minus from 17 which will give you your answer, in this case 5.
17 mod 6=5
Chad FinchChad Finch
Modulus division is pretty simple. It uses the remainder instead of the quotient.
13/12 = 1R1, ergo 13%12 = 1.
It helps to think of modulus as a 'cycle'.
In other words, for the expression
n % 12
, the result will always be < 12.That means the sequence for the set
0..100
for n % 12
is:In that light, the modulus, as well as its uses, becomes much clearer.
Braden BestBraden Best
Easier when your number after the decimal (0.xxx) is short. Then all you need to do is multiply that number with the number after the division.
Ex:
32 % 12 = 8
You do
32/12=2.666666667
Then you throw the 2
away, and focus on the 0.666666667
0.666666667*12=8
<-- That's your answer.(again, only easy when the number after the decimal is short)
Gc220Gc220
The only important thing to understand is that modulus (denoted here by % like in C) is defined through the Euclidean division.
For any two
(d, q)
integers the following is always true:As you can see the value of
d%q
depends on the value ofd/q
. Generally for positive integers d/q
is truncated toward zero, for instance 5/2 gives 2, hence:However for negative integers the situation is less clear and depends on the language and/or the standard. For instance -5/2 can return -2 (truncated toward zero as before) but can also returns -3 (with another language).
In the first case:
![Fpga Fpga](http://www.jps-pcb.com/upfile/2016/12/20161227153000_482.png)
but in the second one:
As said before, just remember the invariant, which is the Euclidean division.
Further details:
Picaud VincentPicaud Vincent
It's simple, Modulus operator(%) returns remainder after integer division. Let's take the example of your question. How 27 % 16 = 11? When you simply divide 27 by 16 i.e (27/16) then you get remainder as 11, and that is why your answer is 11.
Shiraz ShresthaShiraz Shrestha
Write out a table starting with 0.
Continue the table in rows.
Everything in column one is a multiple of 5. Everything in column 2 is a multiple of 5 with 1 as a remainder. Now the abstract part: You can writethat (1) as 1/5 or as a decimal expansion. The modulus operator returns onlythe column, or in another way of thinking, it returns the remainder on long division. You are dealing in modulo(5). Different modulus, different table.Think of a Hash Table.
Terrence McLaughlinTerrence McLaughlin
When we divide two integers we will have an equation that looks like the following:
A/B =Q remainder R
A is the dividend; B is the divisor; Q is the quotient and R is the remainder
Sometimes, we are only interested in what the remainder is when we divide A by B.For these cases there is an operator called the modulo operator (abbreviated as mod).
Examples
See Khan Academy Article for more information.
In Computer science, Hash table uses Mod operator to store the element where A will be the values after hashing, B will be the table size and R is the number of slots or key where element is inserted.
See How does a hash table works for more information
Community♦
Yogendra PaudyalYogendra Paudyal
This was the best approach for me for understanding modulus operator. I will just explain to you through examples.
When you division these two number, remainder is the result. This is the way how i do it.
So what is left to 16 is 1
Here is one more example:
16 % 7 = 7 + 7 = 14
what is left to 16? Is 2
16 % 7 = 2
One more:
24 % 6 = 6 + 6 = 12; 12 + 6 = 18; 18 + 6 = 24
. So remainder is zero, 24 % 6 = 0
NemusNemus
protected by Community♦Apr 28 '15 at 5:08
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?