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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:42:28+00:00 2026-05-31T20:42:28+00:00

I just heard about that x mod (2^32-1) and x / (2^32-1) would be

  • 0

I just heard about that x mod (2^32-1) and x / (2^32-1) would be easy, but how?

to calculate the formula:

xn = (xn-1 + xn-1 / b)mod b.

For b = 2^32, its easy, x%(2^32) == x & (2^32-1); and x / (2^32) == x >> 32. (the ^ here is not XOR). How to do that when b = 2^32 – 1.

In the page https://en.wikipedia.org/wiki/Multiply-with-carry. They say “arithmetic for modulus 2^32 − 1 requires only a simple adjustment from that for 2^32“. So what is the “simple adjustment”?

  • 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-31T20:42:29+00:00Added an answer on May 31, 2026 at 8:42 pm

    (This answer only handles the mod case.)

    I’ll assume that the datatype of x is more than 32 bits (this answer will actually work with any positive integer) and that it is positive (the negative case is just -(-x mod 2^32-1)), since if it at most 32 bits, the question can be answered by

    x mod (2^32-1) = 0 if x == 2^32-1, x otherwise
    x / (2^32 - 1) = 1 if x == 2^32-1, 0 otherwise
    

    We can write x in base 2^32, with digits x0, x1, …, xn. So

      x = x0 + 2^32 * x1 + (2^32)^2 * x2 + ... + (2^32)^n * xn
    

    This makes the answer clearer when we do the modulus, since 2^32 == 1 mod 2^32-1. That is

      x == x0 + 1 * x1 + 1^2 * x2 + ... + 1^n * xn (mod 2^32-1)
        == x0 + x1 + ... + xn (mod 2^32-1)
    

    x mod 2^32-1 is the same as the sum of the base 2^32 digits! (we can’t drop the mod 2^32-1 yet). We have two cases now, either the sum is between 0 and 2^32-1 or it is greater. In the former, we are done; in the later, we can just recur until we get between 0 and 2^32-1. Getting the digits in base 2^32 is fast, since we can use bitwise operations. In Python (this doesn’t handle negative numbers):

    def mod_2to32sub1(x):
        s = 0 # the sum
    
        while x > 0: # get the digits
            s += x & (2**32-1)
            x >>= 32
    
        if s > 2**32-1:
            return mod_2to32sub1(s)
        elif s == 2**32-1:
            return 0
        else:
            return s
    

    (This is extremely easy to generalise to x mod 2^n-1, in fact you just replace any occurance of 32 with n in this answer.)

    (EDIT: added the elif clause to avoid an infinite loop on mod_2to32sub1(2**32-1). EDIT2: replaced ^ with **… oops.)

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

Sidebar

Related Questions

Just curious. I heard something about the new G1GC addressing that latency problem. I
I just heard Joel & Jeff talk about Sender ID in their podcast number
I just heard about phonegap that this is framework which let developers make web
I'm just curious to know about this.When i heard about Spring.net and tried some
I've just upgraded from MRI-1.8.7 to MRI-1.9.1 and heard about this lighthouse ticket which
I just recently heard of duck typing and I read the Wikipedia article about
I have just heard that - push notification is possible in iPhone I need
I have just heard that the iphone cannot do double natively thereby making them
I've heard before that modules are just classes too. I have a few situations,
When I heard about LESS, I originally thought that they're usually pre-processed into css

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.