38

I've been searching around for quite a while, and I've found almost nothing on how BigInteger actually holds its numbers. Are they an array of chars? Something else? And how is data converted to/from BigInteger?

From what I've found, I am assuming that all of arbitrary precision classes, like BigInteger and BigDecimal, hold data as a character array. Is this how it actually works? Or is it just people's guess?

I'm asking because I have been working on my own implementation of something like BigInteger, but I can't figure out how to hold numbers larger than Long.MAX_VALUE (I don't remember the actual number).

Thanks in advance.

jww
  • 97,681
  • 90
  • 411
  • 885
Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
  • 3
    Remember that an implementation is free to do what it likes, as long as it keeps to the JDK's `BigInteger` contract. You can review the full source for the Sun JDK if you try hard enough -- Oracle's made it harder to get than it used to be, but it's still possible; [start here](http://java.sun.com/j2se/1.5.0/source_license.html). You can browse the OpenJDK's version [here](http://hg.openjdk.java.net/jdk6/jdk6-gate/jdk/file/tip/src/share/classes/java/math/BigInteger.java) (it also uses `int[]`). – T.J. Crowder May 04 '11 at 04:55

3 Answers3

34

With an int[]

From the source:

/**
 * The magnitude of this BigInteger, in <i>big-endian</i> order: the
 * zeroth element of this array is the most-significant int of the
 * magnitude.  The magnitude must be "minimal" in that the most-significant
 * int ({@code mag[0]}) must be non-zero.  This is necessary to
 * ensure that there is exactly one representation for each BigInteger
 * value.  Note that this implies that the BigInteger zero has a
 * zero-length mag array.
 */
final int[] mag;
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • 2
    @glowcoder Thanks for the response, but is there any other detail to it? Such as how it converts an input to that array? – Jon Egeland May 04 '11 at 04:43
  • 3
    In layman terms ... (in that particular implementation) *the bit-wise number representation is packed into an array of int's*. –  May 04 '11 at 04:50
  • (Internally two's complement is not used -- although `valueOf(int[])` will accept input in two's complement.) –  May 04 '11 at 04:57
  • 1
    @glowcoder: Oracle goes to some lengths to ensure that people accept a license before having access to the source code. Posting the entire class online without those protections is probably a copyright violation. Instead, simply point @Jon at [where he can get a copy of the source himself](http://java.sun.com/j2se/1.5.0/source_license.html). – T.J. Crowder May 04 '11 at 05:03
  • @glowcoder: What part of the JRL do you interpret as allowing you to post the code to PasteBin? Again, Oracle requires people to go through a series of steps and accept the license (either the SCSL or the JRL). There's a *reason* the code isn't just browsable somewhere. (There was a site that did that. They've had to remove it.) (Separately: "@TJ" is apparently too short to trigger the notification system. "@T.J." [e.g., spelled correctly] is probably long enough, I think "@Crowder" works as well.) – T.J. Crowder May 04 '11 at 06:00
  • 2
    @T.J. Sorry I didn't see the dots there. :) I'd prefer not to use @Crowder, as that happens to be my manager's last name as well, and I'd hate to confuse the two (because I like you and I'd rather it stay that way!) Anyway on topic, I took a look at the license, and you're absolutely right. I removed the link. – corsiKa May 04 '11 at 06:30
-1

The most common way of representing numbers is by using the positional notation system. Numbers are written using digits to represent multiples of powers of the specified base. The base that we are most familiar with and use everyday, is base 10. When we write the number 12345 in base 10, it actually means: 12345 = 1*10^4 + 2*10^3 + 3*10^2 + 4*10^1 + 5*10^0

Continued here...

Petr Abdulin
  • 33,883
  • 9
  • 62
  • 96
-2

There are many ways to represent big integers. Strings of characters is simple, and anyone who has ever done long division with pencil and paper can write the arithmetic routines.

ddyer
  • 1,792
  • 19
  • 26