Question

The source code of BigInteger class has to say this regarding the representation of the magnitude of the BigInteger:

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.

What is meant by minimality of the magnitude in this context? Also how does making the most significant int non zero ensure exactly one representation of each BigInteger value?

Was it helpful?

Solution

Roughly translated, this is saying that BigIntegers should not have leading zeroes in the representation. The word "minimal" is used because a BigInteger represented this way is represented with an array of minimum length (since an array that was identical except for leading zeroes would represent the same BigInteger, and therefore there would not be "exactly one representation for each BigInteger value").

Edited to add: of course, the value 0 can be represented with an array of length 0, thus ensuring that the requirement "most significant int must not be zero" is not violated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top