May 16, 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

  1. The Right Web Album/image/photo Album Host?
    a question regarding the confusion of choosing the right host (7)
  2. Java Exercise Help
    I need help to understand this question (2)
    Ok at my Uni they teach you OOP with java, They used to do with C++ i believe but swiched to java
    because its easier to teach people OOP with java apparently. Anyway I've been working on some
    exercises with java (book is 'objects first with java'). I've got this one exercise
    which I cant understand. QUOTE Exercise 4.29 Rewrite getLot so that it does not rely on a
    lot with a particular number being stored at index (number - 1) in the collection. For instance,
    if lot number 2 has been removed, then lot number 3 will have been moved from index 2 ....
  3. Why Must We Learn Object Oriented Programming
    help me to understand the benefit of OOP (4)
    As a software laboratory assistant, I teach the class of Object Oriented Programming. Everything
    went well until one day a student of mine ask me a simple problem like this Q/A: him : Why must we
    use Object oriented Programming? I thought the usual one could do almost everything. me : Well, in
    Object Oriented Programming we could make a class, so basically eveything could be made to an
    'object' in our mind. him : That's why I'm asking, why must we made everything an
    object? What's the difference? me : In Object Oriented Programming, Encapsulation ....
  4. A Little Help With My Cat?
    Can you guys help me understand why my cat does this strange habit? (4)
    I look after this stray cat,male, which is aroung 4-5 years. It keeps running up to me and rubbing
    itself no matter how long even for hours! It follows me wherever I go unless I enter my house
    and as soon as I stop it starts purring and rubbing again! I am not at all complainig about
    this show of affection but it does intrigue me... /biggrin.gif" style="vertical-align:middle"
    emoid=":D" border="0" alt="biggrin.gif" /> guys, anyone know why this strange thing happens?....
  5. I Dont Understand On How To Use This Forum?
    what can i do? (4)
    /ohmy.gif" style="vertical-align:middle" emoid=":o" border="0" alt="ohmy.gif" /> im so confused?....
  6. I Just Want To Understand Better
    the effect of bandwith on wireless (2)
    OK, so let's say we have an N network, with all N clients, and a Comcast pipe of 8 megabits per
    second. N has a theoretical top speed of 540 mbps, let's say I'm getting 250. But, in
    reality, I'm not getting 250, I'm getting 8. So in a way, unless you have a pipe larger then
    54 mbps (the theoretical max of G), it is useless to upgrade to N. Is that correct or am I missing
    something?....
  7. My Confusion About Dead/non-existence
    (11)
    I've been thinking to myself about this for quite a while now. And finally I'm giving up on
    trying to figure it out for myself. Death. Being dead. Not existing the the human realm. I keep
    trying to imagine if the human race never existed, if I never existed, where would "I" be. As a
    conscience being it's impossible for me to imagine "me" not existing. This is one of the only
    reasons I believe in an after-life. If we died, and were wiped from the face of this..universe,
    where would our conscience be? It's so hard for me to explain this, bear with me here....
  8. I Don't Understand Hosting Credits
    (8)
    Hey, can someone please point out a topic or brief me about the hosting credits? I'm confused.....
  9. Mascot Phobia
    can't understand why some are afraid of mascots (11)
    i've been holding back on this topic in my mind for quite some time: why are some people so
    afraid of mascots? you know, like Ronald McDonald and his friends, etcetera. /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> i have this officemate who
    is so afraid of mascots, that she always tries to hide behind anybody walking along with her when
    she sees a mascot nearby. and she shrieks in fear when the mascot even remotely attempts to approach
    and get near her. i don't understand how even after reaching way past adulthood (she'....
  10. Confusion In The Matrix Revolutions
    (3)
    Ok, ive watched it many many many many mnay times it meaning The Matrix Revolutions.. and i am
    confused as hell (not really) but, in the end of the movie the little girl asks to oracle if they
    will ever see Neo again. and the oracle responds by saying, :Yes i suspect we will; what does this
    mean?? does this mean that they WILL see NEO again? or does this mean that the problem in the
    matrix will restart all over again and they will find a new "The One" to fufill the prophecy, and
    the new "One" will be neo again so they will see him again he just wont know it. ot what....
  11. Did Amd Buy Ati ?
    ... confusion && rumour .... (4)
    There's been a lot of rumor going around for days that AMD is considering to but ATI
    Technologies. Well there's no definite proof, or anything official , but I just came across
    this post in OCWorkbench.com :: QUOTE
    http://www.ocworkbench.com/ocwbcgi/newspro...152063655,35448 ,, According to a news article on
    Sinatech 新浪网, a Chinese daily. It reported that there is news that AMD has
    acquired ATi and it will be officially announced. The person who disclosed this news to the media
    didn't reveal too much details involving the acqui....
  12. A Question About Dinosaurs And Christians
    I dont understand when they would come in? (4)
    I have been wondering this for awhile. I dont understand what they think because im not christian.
    This is not a debate either. OK well here its is. For the Christians who believe the Adam and Eve
    story (The catholic church has accepted evolution) when do dinosaurs come in. You have got to
    believe in them right? I mean they have bones for them. The thing i dont get though is adam even
    were on Earth from the beginning (well one of the seven days i dont remember what one) but then
    there were considered cavemen? Sorry i might be an idiot, but i don't get this at all.....
  13. Nose Hair
    I don't understand them (9)
    Why do we need them. I mean really what is the point anyway. I really don't feel like stick a
    machine up my nostril to cut those annoyingly long hairs just so I look neat and trim. I mean
    evidently it's a gainst manner law to stick your fingers up there in anyway shape or form, or
    you will look like you are picking your nose when in fact you are scracthing it. Now granted there
    are times when you do have a really big crusty up there an dyou need to get it out, and it IS IN
    FACT attached to a nose hair and it all comes out in one single yank, but then you sneeze....
  14. Gnu And Confusion...
    (0)
    Why the hell is almost everything in GNU unextendable why go to all the trouble of creating a highly
    extendable FSH when you need 80 different programs that could be implimented around a single program
    and libraries and don't say "becasue it lowers the point of failure to just one" because to boot
    you use kernel libraries. What a *disappointment*. GNU has alot of standards just nothing what so
    ever to do with application frameworknote=jlhaslip]Edit to replace undesirable words. Please
    remember this is a Public Forum and some of the Public is younger.......
  15. Confusion About Adsense Cpm ?
    (1)
    I want to know somethings about google adsense.I want to know , if i choose their option CPM..(cost
    per 1000 impressions) , it means that they will give 1 $ for 1000 views.But i am confuse that
    they have usually a box , and in that box there are 4 different ads showing.Does it mean that they
    are 4 ads or that box of 3-4 ads is considered as 1 ad ? Also another thing that , what if a same
    user opens the same site many times ( for instant , a user access to the forum) many times, will i
    earn something from that or no ? can any one explain me the idea of CPM ? and any ....
  16. Mmorpgs - Is There An Easy Way To Make Them?
    that's easy to understand? (9)
    I know this may not be a question directly related to RPGs, but I couldn't find a better place
    to put it, so if there is a more appropriate place, please move it for me. Thanks! Ok, I've
    got a question. As an amateur programmer, I have found many ways to make some different games that
    are of mixed levels of professionalism. My favourites are RPGs, and I'm working on this really
    big one that I hope to make into an online game sometime. This has lead me to a very big question.
    Recently I've noticed the shear amount of Rpg's people have made that a....
  17. Css So Only Ie Will Understand It
    (5)
    I have a site that lloks great in FireFox, but there are a few things that need to be tweaked in IE.
    I've seen a bunch of sites that have done something with toeir code to make it work with IE. Is
    there a way I can make FireFox ignore this line {position:relative; left:3px;}?....
  18. Confusion About Hosting Features
    trap17.com seems to contradict itself (3)
    I am a little confused by the trap17.com web site. With regards to the 2 free hosting packages, it
    lists in 3 places 3 different amounts of space and bandwidth. On the right hand side of the site it
    lists the 2 packages and the credits required to earn them. These are the space and bandwidth
    figures I am familar with. What confuses me is lower down in the center section of the site where it
    lists a large range of features.. see below. QUOTE   ::    FREE WEB Hosting Features ::  
    1. Sub-Domain or Domain Hosting   2. Cpanel (latest release updated automatically....
  19. Confusion - A Recalled Song Due To The Mistakes.
    Major mistakes in this song, recalled (2)
    ....
  20. Where Can I Get Uni.cc Domain Names?
    don't understand about dns and uni.cc (3)
    QUOTE You can go for any hosting Option, that you like :- You can use your Existing DOMAIN
    and point it to our servers You can use our sub-domain (highly Recommended) We also allow you to
    use uni.cc domains. But you can always point your uni.cc domain to our sub-domain else you can
    request for DNS based setup. where can i get a free DNS or the uni.cc thing?... /sad.gif'
    border='0' style='vertical-align:middle' alt='sad.gif' /> /blink.gif' border='0'
    style='vertical-align:middle' alt='blink.gif' /> Your titles are lagging. Renamed from What Does....
  21. New To Trap17 Free Hosting
    Help with website (i dont understand...) (4)
    Hello i am new to trap 17's free hosting website....that they hooked up for me. I'm used to
    using phpbb & other forums....and i never used this kind of thing before....so wut im trying to say
    is that i dont understand how to work this. LIke i cant get any thing done cuz it keeps saying
    error's. All i want to do is make a site that can take me to forusm and other things but im new
    to ftp, & stuff if any body could help me set this website up. that would be greatly appreciated.
    When i try to install a phpbb forum i get this error: QUOTE Warning: mysql_er....
  22. Girl I Can't Understand
    HELP!! (39)
    Ok... so I have this ex-girlfriend named Kati. She was a really nice girl while we were dating, very
    nice and sweet. During the end of the relationship she just started to randomly yell and be mean
    towards me. So then she broke up with me. I felt real bad cause she was like perfect to me. I
    realize now that she's not and I have gotten a new girlfriend. Well, I was on myspace a couple
    of days ago and I see she put on new pictures. I look at them and say "wow". She just looks better
    than ever. Amazing to me. I've started to talk to her again and I expected that she....
  23. Unstable Emotional Life
    Is it me or maybe none understand me? (6)
    Well, it seems pretty simple: I dont like long terms - Relationships. I am very in the
    'casual' thing, you know, casual sex, or maybe a bit more than that but not the so called
    'formal relationship'. Most of girls I have dated dont like the idea of getting involved
    with someone who just looks for casual sex. That might represent a proble for most girls in the
    world, but I guess you cant force a relationship, imagine you saying 'well, she's my
    girlfriend' when you just met her a night before on your bed.... I may be wrong but I believe
    I'....
  24. Help Starting Php?
    I don't understand how to begin... (7)
    Well, I don't know where to begin PHP. I understand scripts and everything. But how do I make a
    .PHP file? I am guessing go to the file manager. Create a file called .php. But what type of file
    do I save it as? Perl? I am really lost, but I really want to start learning PHP. Thanks.....
  25. How To Resize/host Images!
    Very easy to understand! (15)
    For the tutorial I'm ganna be using IrfanVeiw. It's a freeware image program which you can
    use to resize and add effect to image, and a lot more ofcourse. If you don't have IrfanVeiw you
    can get it from download.com or anyother download site. How to resize images: First you gatta
    open Irfanveiw...gee, how obvious. First, open the picture you wish to resize. Second, click the tab
    that says "Image" -=Example=- Third, click "Resize/Resample -=Example=- Finaly, set the Height
    and Width of the picture to what you want, make sure you have "Set New Size" a....
  26. Confusion
    New graphic of a miserable guy (10)
    I call it confusion.....
  27. Know
    i don`t understand (4)
    Yeah!....
  28. I don't understand...
    (3)
    How do I change the template on the site to the one I just downloaded?....
  29. *confusion about site*
    (2)
    Please forgive me if there is an obvious way to solve this, I hope my noob innocense is still
    intact. My cpanel says I have unlimited space and doesn't store any updates. Furthermore my ftp
    account is empty. I tried to create a forum, it created the database but not the rest. It won't let
    me adjust anything due to permissions. If I'm doing something stupid, can you please tell me?
    Only I'd quite like my site to work. Thanks a billion, Prophile P.S. If the answer is obvious,
    please don't flame me too much.....
  30. Apparently My Account Has Been Suspended
    I do not understand why though (11)
    "This Account Has Been Suspended" that is what it says at the top of my sub-domain. The Following
    may be the possible reasons ( If you are a Free Hosted Member ) :- 1. You were *inactive* at the
    forums or broken forum rules. 2. Your Account was in violation of TOS. 3. You do not have
    sufficient Hosting Credits or your credits got exhausted. If you are a member, Check out
    http://www.trap17.com/forums/index.php for checking out your hosting credits. 1. You were
    *inactive* at the forums or broken forum rules. - I am almost certain this isn't what happe....

    1. Looking for modulo, operator, confusion, understand

Searching Video's for modulo, operator, confusion, understand
Similar
The Right
Web
Album/image/
photo Album
Host? - a
question
regarding
the
confusion of
choosing the
right host
Java
Exercise
Help - I
need help to
understand
this
question
Why Must We
Learn Object
Oriented
Programming
- help me to
understand
the benefit
of OOP
A Little
Help With My
Cat? - Can
you guys
help me
understand
why my cat
does this
strange
habit?
I Dont
Understand
On How To
Use This
Forum? -
what can i
do?
I Just Want
To
Understand
Better - the
effect of
bandwith on
wireless
My Confusion
About
Dead/non-exi
stence
I Don't
Understand
Hosting
Credits
Mascot
Phobia -
can't
understand
why some are
afraid of
mascots
Confusion In
The Matrix
Revolutions
Did Amd Buy
Ati ? - ...
confusion
&&
rumour ....
A Question
About
Dinosaurs
And
Christians -
I dont
understand
when they
would come
in?
Nose Hair -
I don't
understand
them
Gnu And
Confusion...
Confusion
About
Adsense Cpm
?
Mmorpgs - Is
There An
Easy Way To
Make Them? -
that's
easy to
understand?
Css So Only
Ie Will
Understand
It
Confusion
About
Hosting
Features -
trap17.com
seems to
contradict
itself
Confusion -
A Recalled
Song Due To
The
Mistakes. -
Major
mistakes in
this song,
recalled
Where Can I
Get Uni.cc
Domain
Names? -
don't
understand
about dns
and uni.cc
New To
Trap17 Free
Hosting -
Help with
website (i
dont
understand..
.)
Girl I
Can't
Understand -
HELP!
3;
Unstable
Emotional
Life - Is it
me or maybe
none
understand
me?
Help
Starting
Php? - I
don't
understand
how to
begin...
How To
Resize/host
Images!
- Very easy
to
understand&#
33;
Confusion -
New graphic
of a
miserable
guy
Know - i
don`t
understand
I don't
understand..
.
*confusion
about site*
Apparently
My Account
Has Been
Suspended -
I do not
understand
why though
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