How big is happybks obsessive-compulsive disorder--biginteger?

Source: Internet
Author: User

Now everything's going to be big data. Even an int has become a biginteger. In some scenarios it seems that you have to use it.

Then someone said, "How big is this day?" ”

No, it should be "how big is this BigInteger?" ”

Let's take a look at the internal structure of BigInteger.

In BigInteger, a large int is actually implemented by an int array, which is mag. There is also an int numeric signum, which is used as a plus or minus mark.

Public class biginteger extends number implements comparable<biginteger>  {    /**     * The signum of this  biginteger: -1 for negative, 0 for zero, or     *  1 for positive.  Note that the BigInteger zero <i> Must</i> have     * a signum of 0.  this  is necessary to ensures that there is exactly one      * representation for each BigInteger value.      *     *  @serial      */     Final int signum;    /**     * 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;

So how do you put a number that has a range larger than the int range in the BigInteger mag array?

BigInteger has a lot of overloaded constructors, and we pick one to illustrate it:

    /**     * constructs a biginteger with  the specified value, which may not be zero.      */    private biginteger (Long val)  {         if  (val < 0)  {             val = -val;             signum = -1;        } else {             signum = 1;         }        int highWord =  (int) ( VAL&NBSP;&GT;&GT;&GT;&NBSP;32);        if  (highWord==0)  {             mag = new int[1];             mag[0] =  (int) val;        }  else {            mag = new  int[2];            mag[0] =  highword;            mag[1] =  (int) Val;         }    }

We can see that for a long length of 8 bytes, it is exactly the size of two int. In the above constructor, the first by shifting the 32-bit to the right, to get a long high 32-bit number, if 0, the actual value of long is not more than the range of int, so the mag array only applies to the size of an int, save a long low 32 bits, if not 0, Indicates that the actual value of long has exceeded the range of int, at this time, mag application two int size, will be high 32 bits exist mag[0], low 32 bits exist mag[1].


That is, the presence of a high number of bits precedes the existence of a low number of digits.


Well, back to the original question, how big is BigInteger?

Since mag is an array, its length receives the effect of the subscript range, that is, the number of mag does not exceed the maximum value of int, that is, 2 of 31 square-1, or 2147483647. Then each int is 32 bits. So the number of bits that mag can scale is 2147483647*32.

So, the range of BigInteger should be [-22147483647*32-1 , 22147483647*32-1 -1]




How big is happybks obsessive-compulsive disorder--biginteger?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.