What is the limit of the Value Type BigInteger in C#?

后端 未结 3 1670
走了就别回头了
走了就别回头了 2021-01-01 22:03

As described in MSDN BigInteger is :

An immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower boun

3条回答
  •  醉酒成梦
    2021-01-01 22:16

    TL;DR: BigInteger maxvalue is 2^68719474912

    In .Net 4.7.2 BigInteger uses an uint array for bits.

    An uint holds 32bits of data.

    An array's max size is defined as internal const int MaxByteArrayLength = 0x7FFFFFC7; 7FFFFFC7 = 2147483591

    Now, to calculate: max size of array x capacity of each uint is: 2147483591 x 32 = 68719474912. But that's only the count of the bits in a BigInteger.

    Which means BigInteger's max value is: 2^68'719'474'912 which is stupendusly large (used ' for easier readability).

    If they ever decide to increase the array's max size, then it will also increase the max value for BigInteger.

提交回复
热议问题