Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 36089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:20:18+00:00 2026-05-10T14:20:18+00:00

I read somewhere once that the modulus operator is inefficient on small embedded devices

  • 0

I read somewhere once that the modulus operator is inefficient on small embedded devices like 8 bit micro-controllers that do not have integer division instruction. Perhaps someone can confirm this but I thought the difference is 5-10 time slower than with an integer division operation.

Is there another way to do this other than keeping a counter variable and manually overflowing to 0 at the mod point?

const int FIZZ = 6; for(int x = 0; x < MAXCOUNT; x++) {     if(!(x % FIZZ)) print('Fizz\n'); // slow on some systems } 

vs:

The way I am currently doing it:

const int FIZZ = 6; int fizzcount = 1; for(int x = 1; x < MAXCOUNT; x++) {     if(fizzcount >= FIZZ)      {         print('Fizz\n');         fizzcount = 0;     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:20:19+00:00Added an answer on May 10, 2026 at 2:20 pm

    Ah, the joys of bitwise arithmetic. A side effect of many division routines is the modulus – so in few cases should division actually be faster than modulus. I’m interested to see the source you got this information from. Processors with multipliers have interesting division routines using the multiplier, but you can get from division result to modulus with just another two steps (multiply and subtract) so it’s still comparable. If the processor has a built in division routine you’ll likely see it also provides the remainder.

    Still, there is a small branch of number theory devoted to Modular Arithmetic which requires study if you really want to understand how to optimize a modulus operation. Modular arithmatic, for instance, is very handy for generating magic squares.

    So, in that vein, here’s a very low level look at the math of modulus for an example of x, which should show you how simple it can be compared to division:


    Maybe a better way to think about the problem is in terms of number bases and modulo arithmetic. For example, your goal is to compute DOW mod 7 where DOW is the 16-bit representation of the day of the week. You can write this as:

     DOW = DOW_HI*256 + DOW_LO   DOW%7 = (DOW_HI*256 + DOW_LO) % 7        = ((DOW_HI*256)%7  + (DOW_LO % 7)) %7        = ((DOW_HI%7 * 256%7)  + (DOW_LO%7)) %7        = ((DOW_HI%7 * 4)  + (DOW_LO%7)) %7 

    Expressed in this manner, you can separately compute the modulo 7 result for the high and low bytes. Multiply the result for the high by 4 and add it to the low and then finally compute result modulo 7.

    Computing the mod 7 result of an 8-bit number can be performed in a similar fashion. You can write an 8-bit number in octal like so:

      X = a*64 + b*8 + c 

    Where a, b, and c are 3-bit numbers.

      X%7 = ((a%7)*(64%7) + (b%7)*(8%7) + c%7) % 7       = (a%7 + b%7 + c%7) % 7       = (a + b + c) % 7 

    since 64%7 = 8%7 = 1

    Of course, a, b, and c are

      c = X & 7   b = (X>>3) & 7   a = (X>>6) & 7  // (actually, a is only 2-bits). 

    The largest possible value for a+b+c is 7+7+3 = 17. So, you’ll need one more octal step. The complete (untested) C version could be written like:

    unsigned char Mod7Byte(unsigned char X) {     X = (X&7) + ((X>>3)&7) + (X>>6);     X = (X&7) + (X>>3);      return X==7 ? 0 : X; } 

    I spent a few moments writing a PIC version. The actual implementation is slightly different than described above

    Mod7Byte:        movwf        temp1        ;        andlw        7        ;W=c        movwf        temp2        ;temp2=c        rlncf   temp1,F        ;        swapf        temp1,W ;W= a*8+b        andlw   0x1F        addwf        temp2,W ;W= a*8+b+c        movwf        temp2   ;temp2 is now a 6-bit number        andlw   0x38    ;get the high 3 bits == a'        xorwf        temp2,F ;temp2 now has the 3 low bits == b'        rlncf   WREG,F  ;shift the high bits right 4        swapf   WREG,F  ;        addwf        temp2,W ;W = a' + b'   ; at this point, W is between 0 and 10          addlw        -7        bc      Mod7Byte_L2 Mod7Byte_L1:        addlw        7 Mod7Byte_L2:        return 

    Here’s a liitle routine to test the algorithm

           clrf    x        clrf    count  TestLoop:        movf        x,W        RCALL   Mod7Byte        cpfseq count         bra    fail         incf        count,W        xorlw   7        skpz         xorlw        7        movwf   count         incfsz        x,F        bra        TestLoop passed: 

    Finally, for the 16-bit result (which I have not tested), you could write:

    uint16 Mod7Word(uint16 X) {  return Mod7Byte(Mod7Byte(X & 0xff) + Mod7Byte(X>>8)*4); } 

    Scott


    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 95k
  • Answers 95k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer see http://dev.mysql.com/doc/refman/5.0/en/external-locking.html quote: If you run multiple servers that use… May 11, 2026 at 7:04 pm
  • Editorial Team
    Editorial Team added an answer If you're targeting iPhone OS 2.2 or later, you can… May 11, 2026 at 7:04 pm
  • Editorial Team
    Editorial Team added an answer You can do so with WITH: WITH subquery AS( SELECT… May 11, 2026 at 7:04 pm

Related Questions

Is it possible to access a USB drive or Flash card without using the
I’ve got a requirement to create tabs for content displayed on a web page,
Say I have a class called PermissionManager which should only exist once for my
I'm writing an Applescript playlist generator. Part of the process is to read the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.