Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 348
NSWOA Avatar asked Apr 18 '10 22:04

NSWOA


People also ask

How do you do division modulus?

To calculate modulo division: Subtract the divisor from the dividend until the resultant is less than the divisor.

How does the modulus operator work?

The modulus operator, sometimes also called the remainder operator or integer remainder operator works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign ( % ). The syntax is the same as for other operators.

What is modulus division in programming?

The modulo division operator produces the remainder of an integer division. Syntax: If x and y are integers, then the expression: x % y. produces the remainder when x is divided by y. Return Value: If y completely divides x, the result of the expression is 0.

What is the meaning of 1 mod 3?

Answer: 1 mod 3 is 1. Let's find 1 mod 3. Explanation: 1 mod 3 equals 1, since 1/3 = 0 with a remainder of 1. To find 1 mod 3 using the modulus method, we first find the highest multiple of the divisor, 3 that is equal to or less than the dividend, 1.


16 Answers

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!

like image 92
Marcin M. Jessa Avatar answered Oct 24 '22 03:10

Marcin M. Jessa


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
like image 32
Leo Avatar answered Oct 24 '22 03:10

Leo


The simple formula for calculating modulus is :-

[Dividend-{(Dividend/Divisor)*Divisor}]

So, 27 % 16 :-

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

like image 43
Code_Jamer Avatar answered Oct 24 '22 01:10

Code_Jamer


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

This is exactly what modulo does:

15 / 12 = 1, remainder 3

You find this example better explained on wikipedia: Wikipedia Modulo Article

like image 22
Prine Avatar answered Oct 24 '22 02:10

Prine


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.

like image 36
RebelPhoenix Avatar answered Oct 24 '22 02:10

RebelPhoenix


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

like image 28
chris Avatar answered Oct 24 '22 02:10

chris


Very simple: a % b is defined as the remainder of the division of a by b.

See the wikipedia article for more examples.

like image 36
Yuval Adam Avatar answered Oct 24 '22 01:10

Yuval Adam


I would like to add one more thing:

it's easy to calculate modulo when dividend is greater/larger than divisor

dividend = 5 divisor = 3

5 % 3 = 2

3)5(1
  3
-----
  2

but what if divisor is smaller than dividend

dividend = 3 divisor = 5

3 % 5 = 3 ?? how

This is because, since 5 cannot divide 3 directly, modulo will be what dividend is

like image 24
bn00d Avatar answered Oct 24 '22 01:10

bn00d


I hope these simple steps will help:

20 % 3 = 2 
  1. 20 / 3 = 6; do not include the .6667 – just ignore it
  2. 3 * 6 = 18
  3. 20 - 18 = 2, which is the remainder of the modulo
like image 23
mustafe Avatar answered Oct 24 '22 01:10

mustafe


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)

like image 33
Gc220 Avatar answered Oct 24 '22 01:10

Gc220


27 % 16 = 11

You can interpret it this way:

16 goes 1 time into 27 before passing it.

16 * 2 = 32.

So you could say that 16 goes one time in 27 with a remainder of 11.

In fact,

16 + 11 = 27

An other exemple:

20 % 3 = 2

Well 3 goes 6 times into 20 before passing it.

3 * 6 = 18

To add-up to 20 we need 2 so the remainder of the modulus expression is 2.

like image 25
uwponcel Avatar answered Oct 24 '22 01:10

uwponcel


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:

d = ( d / q ) * q + ( d % q )

As you can see the value of d%q depends on the value of d/q. Generally for positive integers d/q is truncated toward zero, for instance 5/2 gives 2, hence:

5 = (5/2)*2 + (5%2) => 5 = 2*2 + (5%2) => 5%2 = 1

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:

-5 = (-5/2)*2 + (-5%2) => -5 = -2*2 + (-5%2) => -5%2 = -1

but in the second one:

-5 = (-5/2)*2 + (-5%2) => -5 = -3*2 + (-5%2) => -5%2 = +1

As said before, just remember the invariant, which is the Euclidean division.

Further details:

  • What is the behavior of integer division?
  • Division and Modulus for Computer Scientists
like image 28
Picaud Vincent Avatar answered Oct 24 '22 01:10

Picaud Vincent


Modulus division gives you the remainder of a division, rather than the quotient.

like image 22
samoz Avatar answered Oct 24 '22 03:10

samoz


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.

like image 35
Shiraz Shrestha Avatar answered Oct 24 '22 03:10

Shiraz Shrestha


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

like image 37
Chad Finch Avatar answered Oct 24 '22 03:10

Chad Finch


Modulus division is pretty simple. It uses the remainder instead of the quotient.

    1.0833... <-- Quotient
   __
12|13
   12
    1 <-- Remainder
    1.00 <-- Remainder can be used to find decimal values
     .96
     .040
     .036
     .0040 <-- remainder of 4 starts repeating here, so the quotient is 1.083333...

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:

{0,1,2,3,4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7,8,9,10,11,0,[...],4}

In that light, the modulus, as well as its uses, becomes much clearer.

like image 32
Braden Best Avatar answered Oct 24 '22 02:10

Braden Best