Jul 27, 2008

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

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Others

free web hosting

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

sonesay
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"

QUOTE

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.

 

 

 


Reply

rvalkass
QUOTE(sonesay @ Jan 11 2008, 09:27 AM) *
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.

QUOTE(sonesay @ Jan 11 2008, 09:27 AM) *
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.

QUOTE(sonesay @ Jan 11 2008, 09:27 AM) *
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.

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : modulo operator confusion understand


    Looking for modulo, operator, confusion, understand

Searching Video's for modulo, operator, confusion, understand
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE