Search code examples
javabigintegerbitbigint

How are BigInteger stored


I need to generate 512 bit BigInts, but I'm not sure which of the two below is true:

512 bits means 512 digits of 1010101010...001010 which are then converted to the decimal it represents?

Or does it mean 512 digits of 0-9, so basicly a 512-digit number with digits ranging from 0-9? Something like 12414124124....54543=512 digits.


Solution

  • From sourcecode, they are stored in an int array

    The magnitude of this BigInteger, in big-endian 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 (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.

    118 
    119     int[] mag;
    120 
    121     // These "redundant fields" are initialized with recognizable nonsense
    122     // values, and cached the first time they are needed (or never, if they
    123     // aren't needed).
    124