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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:24:04+00:00 2026-06-05T04:24:04+00:00

Is it possible to parse a negative number into an unsigned value with Java’s

  • 0

Is it possible to parse a negative number into an unsigned value with Java’s BigInteger?

So for instance, I’d to interpret -1 as FFFFFFFFFFFFFFFF.

  • 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-05T04:24:07+00:00Added an answer on June 5, 2026 at 4:24 am

    If you are thinking of a two’s complement, you must specify a working bit length. A Java long has 64 bits, but a BigInteger is not bounded.

    You could do something as this:

    // Two's complement reference: 2^n . 
    // In this case, 2^64 (so as to emulate a unsigned long)
    private static final BigInteger TWO_COMPL_REF = BigInteger.ONE.shiftLeft(64);
    
    public static BigInteger parseBigIntegerPositive(String num) {
        BigInteger b = new BigInteger(num);
        if (b.compareTo(BigInteger.ZERO) < 0)
            b = b.add(TWO_COMPL_REF);
        return b;
    }
    
    public static void main(String[] args) {
        System.out.println(parseBigIntegerPositive("-1").toString(16));
    }
    

    But this would implicitly mean that you are working with BigIntegers in the 0 – 2^64-1 range.

    Or, more general:

    public static BigInteger parseBigIntegerPositive(String num,int bitlen) {
        BigInteger b = new BigInteger(num);
        if (b.compareTo(BigInteger.ZERO) < 0)
            b = b.add(BigInteger.ONE.shiftLeft(bitlen));
        return b;
    }
    

    To make it more fooproof, you could add some checks, eg

    public static BigInteger parseBigIntegerPositive(String num, int bitlen) {
        if (bitlen < 1)
            throw new RuntimeException("Bad bit length:" + bitlen);
        BigInteger bref = BigInteger.ONE.shiftLeft(bitlen);
        BigInteger b = new BigInteger(num);
        if (b.compareTo(BigInteger.ZERO) < 0)
            b = b.add(bref);
        if (b.compareTo(bref) >= 0 || b.compareTo(BigInteger.ZERO) < 0 )
            throw new RuntimeException("Out of range: " + num);
        return b;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to parse XML retrieved from a web address into a custom
Possible Duplicate: Parse query string into an array What's the fastest method , to
Is it possible to parse an XSD schema into a JDOM tree? Or is
Possible Duplicate: Parse error on explode('-','foo-bar')[0] (for instance) In PHP there are functions that
Possible Duplicate: Parse query string into an array How can I explode a string
Possible Duplicate: Parse string into argv/argc I'm trying to write a fake shell using
How is it possible to parse command-line arguments that are to be interpreted as
Is it possible to parse the enetered text in the textinput,i want to find
Is it possible to parse HTML (HTML/HTML5, not XHTML) pages using XPath and Qt
Possible Duplicate: How can I parse JSON in Google App Engine? json module was

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.