Jon Jon - 1 year ago 79
Java Question

How does BigInteger store its data?

I've been searching around for quite a while, and I've found almost nothing on how

actually holds its numbers. Are they an array of chars? Something else? And how is data converted to/from

From what I've found, I am assuming that all of arbitrary precision classes, like
, 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
, but I can't figure out how to hold numbers larger than
(I don't remember the actual number).

Thanks in advance.

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download