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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:20:12+00:00 2026-06-08T18:20:12+00:00

The code below takes a BigInteger n and finds a number less than n

  • 0

The code below takes a BigInteger n and finds a number less than n that is also a power of 2. It works fine with small numbers but the first branch of the if statement does not work for int.MaxValue and above. Apparently subtracting 1 (BigInteger.Log(n - 1)) is not enough for larger numbers.

How can I calculate a number to subtract that is large enough to make a difference and yet work on smaller numbers too?

public BigInteger FindNearestPowerOfTwo (BigInteger n)
{
    double number = 0;
    BigInteger big = 0;

    if (n.IsPowerOfTwo)
    {
        number = BigInteger.Log(n - 1) / BigInteger.Log(2);
    }
    else
    {
        number = BigInteger.Log(n) / BigInteger.Log(2);
    }

    number = Math.Floor(number);

    big = BigInteger.Pow(2, (int) number);

    return (big);
}
  • 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-08T18:20:13+00:00Added an answer on June 8, 2026 at 6:20 pm

    In the if branch, you can subtract something from the quotient,

    number = BigInteger.Log(n)/BigInteger.Log(2) - 0.9;
    

    for example (I’d be careful with subtracting 1.0 there since BigInteger.Log(x) isn’t exact, the quotient could be a little too small, then subtracting 1.0 would give you the wrong power of 2). That works also for pretty large numbers (but a double has only 53 bits of precision, so for numbers larger than 2^(2^54) that is guaranteed to fail – however, that’s a lot more memory than currently available).

    But easier would of course be

    if (n.IsPowerOfTwo) {
        return n/2;
    }
    

    However, the else branch is problematic. If n is very close to a power of 2,

    BigInteger.Log(n) / BigInteger.Log(2)
    

    can be a little too large or too small, moving the quotient across the closest integer to the exact result. You should check that big is indeed smaller than n and if not, divide by 2.

    It may be that BigInteger.Log(n, 2.0) produces more exact results than dividing two natural logarithms. (I don’t know the implementation.)

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

Sidebar

Related Questions

the code below works fine but it takes an absolute age to run. How
This code below takes in an two digit number (let's say 12) in ASCII
I have a bit of f# code (below) that takes a unit->obj and evaluates
I am using PHP 5.3.6 I have the following code below. Everything works fine
Consider this F# code to sum the numbers below i that are multiples of
The code below takes an array value, if it's key exist it should echo
In the code below, why it is that when I take the address of
Using the code below each image download) file_get_contents() ) takes on average 8-15 seconds.....
List New Class takes up memory? Do I need C=null; in the code below?
My code below takes a URL inputted by the user and prints out the

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.