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

  • SEARCH
  • Home
  • 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 7072611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:49:47+00:00 2026-05-28T05:49:47+00:00

Specifically: I have two unsigned integers (a,b) and I want to calculate (a*b)%UINT_MAX (UINT_MAX

  • 0

Specifically: I have two unsigned integers (a,b) and I want to calculate (a*b)%UINT_MAX (UINT_MAX is defined as the maximal unsigned int). What is the best way to do so?

Background: I need to write a module for linux that will emulate a geometric sequence, reading from it will give me the next element (modulo UINT_MAX), the only solution I found is to add the current element to itself times, while adding is done using the following logic:(that I use for the arithmetic sequence)

for(int i=0; i<b; ++i){
  if(UINT_MAX - current_value > difference) {
    current_value += difference;
  } else {
    current_value = difference - (UINT_MAX - current_value);
  }

when current_value = a in the first iteration (and is updated in every iteration, and difference = a (always).
Obviously this is not a intelligent solution.
How would an intelligent person achieve this?

Thanks!

  • 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-05-28T05:49:48+00:00Added an answer on May 28, 2026 at 5:49 am

    As has been mentioned, if you have a type of twice the width available, just use that, here

    (unsigned int)(((unsigned long long)a * b) % UINT_MAX)
    

    if int is 32 bits and long long 64 (or more). If you have no larger type, you can split the factors at half the bit-width, multiply and reduce the parts, finally assemble it. Illustrated for 32-bit unsigned here:

    a_low = a & 0xFFFF;  // low 16 bits of a
    a_high = a >> 16;    // high 16 bits of a, shifted in low half
    b_low = b & 0xFFFF;
    b_high = b >> 16;
    /*
     * Now a = (a_high * 65536 + a_low), b = (b_high * 65536 + b_low)
     * Thus a*b = (a_high * b_high) * 65536 * 65536
     *          + (a_high * b_low + a_low * b_high) * 65536
     *          + a_low * b_low
     *
     * All products a_i * b_j are at most (65536 - 1) * (65536 - 1) = UINT_MAX - 2 * 65536 + 2
     * The high product reduces to
     * (a_high * b_high) * (UINT_MAX + 1) = (a_high * b_high)
     * The middle products are a bit trickier, but splitting again solves:
     * m1 = a_high * b_low;
     * m1_low = m1 & 0xFFFF;
     * m1_high = m1 >> 16;
     * Then m1 * 65536 = m1_high * (UINT_MAX + 1) + m1_low * 65536 = m1_high + m1_low * 65536
     * Similar for a_low * b_high
     * Finally, add the parts and take care of overflow
     */
    m1 = a_high * b_low;
    m2 = a_low * b_high;
    m1_low = m1 & 0xFFFF;
    m1_high = m1 >> 16;
    m2_low = m2 & 0xFFFF;
    m2_high = m2 >> 16;
    result = a_high * b_high;
    temp = result + ((m1_low << 16) | m1_high);
    if (temp < result)    // overflow
    {
        result = temp+1;
    }
    else
    {
        result = temp;
    }
    if (result == UINT_MAX)
    {
        result = 0;
    }
    // I'm too lazy to type out the rest, you get the gist, I suppose.
    

    Of course, if what you need is actually reduction modulo UINT_MAX + 1, as @Toad assumes,then that’s just what multiplication of unsigned int does.

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

Sidebar

Related Questions

Suppose I have a table whose function is specifically to link two other tables
I have two vectors of floats, x and y, and I want to compute
I'd like to have two structures which point to eachother. Specifically, I'd like to
What are your thoughts about SQL Server's symmetric key functions? Specifically, I have two
Specifically, let's assume that we have two sensible models: TieDyeCentipede , which has_many :legs
In a nutshell I have two images I want to overlay one over the
Specifically, I have two lists of strings that I'd like to combine into a
I need help with a regular expression for use with UrlRewriting.Net. I have two
I have a spec which reads the next two bytes are signed int. To
I have two VS C# projects (specifically, for an Outlook plugin) that I believe

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.