Jump to content
xisto Community
sonesay

Modulo Operator Confusion I need help to understand what it does exactly

Recommended Posts

Ok I've been doing some revision and came across this operator again. I've seen it before but havent fully understood what it does or its surpose to do.

Taken from "Objects first in java 2006"

The modulo operator
The last method in the NumberDisplay class increments the display value by 1. It takes care that the value resets to zero when the limit is reached:

public void increment()
{
value = (value + 1) % limit;
}

This method uses the modulo operator (%). The modulo operator calculates the remainder of an integer division. For example, the result of the division

27 / 4

can be expressed in integer numbers as

result = 6 remainder = 3

The modulo operator returns just the remainder of such a division. Thus the result of the expression ( 27 % 4) would be 3.


The example it gives (27 / 4) and the increment function to me dont seem to relate and this is where I'm confused as to why or how modulo is suppose to work. The increment function will increase 'value' by 1 everytime its run untill it reaches an int 'limit' then resets to zero. Well thats how it works but I'm still confused.

For another reason the example is (27 / 4) where as the function would be ((lower number + 1) % limit ). A lower number on the left side % by limit which is a higher number.

/sigh someone help explain this to me so I can figure this out.
Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

The example it gives (27 / 4) and the increment function to me dont seem to relate and this is where I'm confused as to why or how modulo is suppose to work.

Modulo is what most people know as "child's division". When you are first taught to divide, you are told to give an answer like "6 remainder 2". Modulo returns the remainder. As far as I can tell, you understand this operation, and what is actually outputted when you use modulo? What you don't understand is how it is being used in the counter.

 

The increment function will increase 'value' by 1 everytime its run untill it reaches an int 'limit' then resets to zero. Well thats how it works but I'm still confused.

Lets go for an example. We'll start with value set to 0 and limit set to 7, OK? Good. Below I'll list what the code does and what output you'll get. Perhaps you'll spot the pattern and why it works:

value = (0+1)%7 = 1%7 1/7 = 0 remainder 1 OUTPUT: 1

value = (1+1)%7 = 2%7 2/7 = 0 remainder 2 OUTPUT: 2

value = (2+1)%7 = 3%7 3/7 = 0 remainder 3 OUTPUT: 3

value = (3+1)%7 = 4%7 4/7 = 0 remainder 4 OUTPUT: 4

value = (4+1)%7 = 5%7 5/7 = 0 remainder 5 OUTPUT: 5

value = (5+1)%7 = 6%7 6/7 = 0 remainder 6 OUTPUT: 6

value = (6+1)%7 = 7%7 7/7 = 1 remainder 0 OUTPUT: 0

value = (0+1)%7 = 1%7 8/7 = 0 remainder 1 OUTPUT: 1

Continuing in an infinite loop...

As value is increased each time, the number you are dividing by 7 also increases. As this happens, the remainder you get also increases. When you reach a number that is in the 7 times table, there is no remainder (as it divides exactly by 7) so the counter appears to reset to 0. This also resets 'value' to 0, and the sequence begins again.

 

For another reason the example is (27 / 4) where as the function would be ((lower number + 1) % limit ). A lower number on the left side % by limit which is a higher number.

The lower number divided by a large number is shown in my example above. The large number goes into the small number 0 times, leaving a remainder equal to the small number.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.