Modulus Operator

This is a discussion on Modulus Operator within the C Programming forums, part of the General Programming Boards category; Hi all, I'm looking at the modulus operator and having a hard time understanding its practical purpose. The book shows ...

  1. #1
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22

    Lightbulb Modulus Operator

    Hi all,

    I'm looking at the modulus operator and having a hard time understanding its practical purpose. The book shows me:

    Code:
    #include <stdio.h>
    {
         int a = 25, b = 5, c = 10, d = 7;
    
         printf ("a %% b = %i\n, a % b);
         printf ("a %% c = %i\n, a % c);
         printf ("a %% d = %i\n", a % d);
         printf ("a/d*d+a %% d = %i\n", a /d*d+a % d);
    
         return 0;
    }
    While I understand what is happening here, I have a hard time invisioning other practical programming situations where this operator is useful. Can any one provide practical examples, and use common language to better explain why the modulus operator is useful?

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It's used a lot in cryptography, because it allows you to perform a math operation and have a fixed-size result. For instance, you may need to multiply 32-bit numbers, and end up with a 32 bit-result. In regular arithmetic, you could use as much as 64 bits, but performing the operation mod 2^32 ensures it will fit in with the rest of the system. That's a very difficult operation to reverse, but a very easy one to verify - which makes it useful for security (see discrete logarithms, etc...). This is also sometimes accomplished using the size of the data type. If you multiply 2 32-bit integers together and try to store the result in another 32-bit integer, the result is automatically mod 2^32, without having to perform any additional math.

    It also provides circular arithmetic. If you have a schedule that rotates between 4 people, it's easy to know who's turn it is on a given day if you take the day number mod 4.

    Say you want a random number between 1 and 10. The C standard library has a function to provide random numbers, but the range is pretty much any integer. If you take any integer mod 10 plus one, you end up with a number between 1 and 10. (For the sake of correctness, I should point out that you end up with a slight bias, since 10 doesn't fit evenly into the range of an integer - but you get the idea).

    There are many others - but that should give you a taste. Rest assured - it is very useful!

  3. #3
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    138
    Another example is when converting an integer (short, int, or long) to a string such as is done when implementing some of the conversions in fprintf() or snprintf() or related functions.

  4. #4
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22
    I knew I could find the answers I was looking for here, Thanks all :-)

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    This statement...

    Code:
    int x = (14 % 6);
    ...assigns x to the remainder of fourteen divided by six. Therefore, x == 2.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    As a side note, I'd be careful using the term "remainder". Some languages have both a 'remainder' and a 'modulus' operator, and they behave slightly differently when it comes to negative numbers. It's important to know the difference and what the language being used uses. Wikipedia has a good table of the operators and their behavior.

    Modulo operation - Wikipedia, the free encyclopedia

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    22
    It's also commonly used in formatting output with a lot of data when printed with iterations.

    If you needed to display, say 10,000 numbers, % c could mean to place a newline every c numbers, meaning to list c columns of numbers every line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. modulus operator with double operands?
    By chickenandfries in forum C Programming
    Replies: 7
    Last Post: 03-28-2008, 07:21 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21