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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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:
But this would implicitly mean that you are working with BigIntegers in the 0 – 2^64-1 range.
Or, more general:
To make it more fooproof, you could add some checks, eg