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 7883029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:25:35+00:00 2026-06-03T04:25:35+00:00

I need to create an algorithm implemented in C that do modulo arithmetic between

  • 0

I need to create an algorithm implemented in C that do modulo arithmetic between an arbitrary number of bytes and one byte. See this:

typedef struct{
    u_int8_t * data;
    u_int16_t length;
}UBigInt;
u_int8_t UBigIntModuloWithUInt8(UBigInt a,u_int8_t b){

}

For powers of two a & (b-1) can be used but what about non powers of two?

I realise one method is: a – b*(a/b)

That would require to use UBigIntDivisionWithUInt8 and UBigIntMultiplicationWithUInt8 and UBigIntSubtractionWithUBigInt. There might be a more efficient way to do this?

Thank you.

This is the implementation I now have:

u_int8_t UBigIntModuloWithUInt8(UBigInt a,u_int8_t b){
    if (!(b & (b - 1)))
        return a.data[a.length - 1] & b - 1; // For powers of two this can be done
    // Wasn't a power of two.
    u_int16_t result = 0; // Prevents overflow in calculations
    for(int x = 0; x < a.length; x++) {
        result *= (256 % b);
        result %= b;
        result += a.data[x] % b;
        result %= b;
    }
    return result;
}
  • 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. Editorial Team
    Editorial Team
    2026-06-03T04:25:36+00:00Added an answer on June 3, 2026 at 4:25 am

    You can use a variation on Horner’s method.
    Process a byte by byte with this formula:
    a % b = ((a // 256) % b) * (256 % b) + (a % 256) % b, where x // y is the rounding division (normal C integer division). The reason this will work is that congruence modulo b is an equivalence relation.
    With this you have an O(length) algorithm, or O(log(a)).
    Example snippet (untested, my C skills are rusty):

    u_int16_t result = 0; // Just in case, to prevent overflow
    for(i = 0, i<a.length; i++) {
        result *= (256 % b);
        result %= b;
        result += (a[i] % b);
        result %= b;
    }
    

    Some justification:
    a = (a // 256) * 256 + (a % 256), therefore
    a % b = ((a // 256) * 256) % b + ((a % 256) % b). However a % 256 = a[n-1] and a // 256 = a[0 .. n-2]. Reversing the actions in a way similar to Horner’s rule gives you the presented snippet.

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

Sidebar

Related Questions

I need to create this function flat that's supposed to re-contract a new list
I need to create a non sequential list of numbers that fit within a
I need to create EAN 8 bar code programmatically. I search an algorithm to
I would need to create a php file that will do some work on
I have an algorithm where I create two bi-dimensional arrays like this: TYPE TPtrMatrixLine
I need to create a thumbnail image with transparent rounded corners. Before this requirement
I need some ideas how to create a activation algorithm. For example i have
I need to create 5 processes (not threads) and synchronize between them with semaphors.
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution

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.