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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:46:57+00:00 2026-05-16T11:46:57+00:00

I have a scenario where I’m working with large integers (e.g. 160 bit), and

  • 0

I have a scenario where I’m working with large integers (e.g. 160 bit), and am trying to create the biggest possible unsigned integer that can be represented with an n bit number at run time. The exact value of n isn’t known until the program has begun executing and read the value from a configuration file. So for example, n might be 160, or 128, or 192, etcetera…

Initially what I was thinking was something like:

BigInteger.valueOf((long)Math.pow(2, n));

but then I realized, the conversion to long that takes place sort of defeats the purpose, given that long is not comprised of enough bits in the first place to store the result. Any suggestions?

  • 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-16T11:46:57+00:00Added an answer on May 16, 2026 at 11:46 am

    On the largest n-bit unsigned number

    Let’s first take a look at what this number is, mathematically.

    In an unsigned binary representation, the largest n-bit number would have all bits set to 1. Let’s take a look at some examples:

    1(2)= 1 =21 – 1
    11(2)= 3 =22 – 1
    111(2)= 7 =23 – 1
    :
    1………1(2)=2n -1
       n

    Note that this is analogous in decimal too. The largest 3 digit number is:

    103- 1 = 1000 - 1 = 999

    Thus, a subproblem of finding the largest n-bit unsigned number is computing 2n.


    On computing powers of 2

    Modern digital computers can compute powers of two efficiently, due to the following pattern:

    20= 1(2)
    21= 10(2)
    22= 100(2)
    23= 1000(2)
    :
    2n= 10………0(2)
           n

    That is, 2n is simply a number having its bit n set to 1, and everything else set to 0 (remember that bits are numbered with zero-based indexing).


    Solution

    Putting the above together, we get this simple solution using BigInteger for our problem:

    final int N = 5;
    BigInteger twoToN   = BigInteger.ZERO.setBit(N);
    BigInteger maxNbits = twoToN.subtract(BigInteger.ONE);
    
    System.out.println(maxNbits); // 31
    

    If we were using long instead, then we can write something like this:

    // for 64-bit signed long version, N < 64
    System.out.println(
        (1L << N) - 1
    ); // 31
    

    There is no “set bit n” operation defined for long, so traditionally bit shifting is used instead. In fact, a BigInteger analog of this shifting technique is also possible:

    System.out.println(
        BigInteger.ONE.shiftLeft(N).subtract(BigInteger.ONE)
    ); // 31
    

    See also

    • Wikipedia/Binary numeral system
    • Bit Twiddling Hacks

    Additional BigInteger tips

    BigInteger does have a pow method to compute non-negative power of any arbitrary number. If you’re working in a modular ring, there are also modPow and modInverse.

    You can individually setBit, flipBit or just testBit. You can get the overall bitCount, perform bitwise and with another BigInteger, and shiftLeft/shiftRight, etc.

    As bonus, you can also compute the gcd or check if the number isProbablePrime.

    ALWAYS remember that BigInteger, like String, is immutable. You can’t invoke a method on an instance, and expect that instance to be modified. Instead, always assign the result returned by the method to your variables.

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

Sidebar

Related Questions

I have a path defined: when /the admin home\s?page/ /admin/ I have scenario that
I have a scenario which I'm a bit stuck on. Let's say I have
I have a scenario where: there are two (or more) tables that represent independent
I have a scenario that looks like this: User A is a registered user.
I have a scenario where I am trying to login a user to my
I have a scenario where I have a bunch of jobs that I am
I have a scenario that does not seem to be covered in any of
I have a scenario wherein with the following details: I have a form that
I have scenario, I have two update panels on the page (both have update
I have a scenario where I have to check whether user has already opened

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.