Java source Analytic integer method Interpretation _java

Source: Internet
Author: User
Tags int size

Interpretation of tounsignedstring method

See in the integer there is such a method to convert int to unsigned type string, but a few points are not very clear, after the query data to understand, read as follows:

 /**
   * Convert the integer to a unsigned number.
   * *
  private static String tounsignedstring (int i, int shift) {
    char[] buf = new CHAR[32];
    int charpos =;
    int radix = 1 << shift;
    int mask = radix-1;
    Do {
      Buf[--charpos] = digits[i & mask];
      I >>>= shift;
    while (i!= 0);

    return new String (BUF, Charpos, (32-charpos));
  }

Here's the parameter shift is the representation of the system, if it is binary, shift is 2, octal is 8, the corresponding mask is calculated as 1 and 7. The corresponding characters in the digits array are constantly removed by mask and I phase.

In the case where I do the logical right shift every time, the highest bit is supplemented by 0, so that I will eventually move to the right after the continuous logical shift to 0.

In addition, the use of do-while is to prevent I itself is 0, the BUF array cannot get its value.

An interpretation of tostring method

This array represents the 10-bit portion of the number, which is used below. Final static char [] Digittens = {' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 1 ', ' 1 ', ' 1 ', ' 1 ', ' 1, ' 1 ', ' 1 ', ' 1 ', ' 1 ', ' 1 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ' ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5, ' 5 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 8 ', ' 8 ', ' 8 ', ' 8, ' 8 ', '

8 ', ' 8 ', ' 8 ', ' 8 ', ' 8 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ', ' 9 ',}; This array represents the single-digit part of the number, and the array is used below.

  If you combine each part of an array, you can get two-bit integers of all the cases within 100. Final static char [] digitones = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4, ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ' ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1, ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4, ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ',}

  ;              
    public static String toString (int i) {if (i = = integer.min_value)//Here's Add 1, start not very clear what meaning, later found negative words You need to add a minus sign to the front, so the size of the string is 1.//The portion of the incoming stringsize is positive, in the following array//map int size = (I < 0)?
    Stringsize (i) + 1:stringsize (i);
    char[] buf = new Char[size];
    GetChars (i, size, buf);
  return new String (0, size, buf);
    } static void GetChars (int i, int index, char[] buf) {int q, r;
    int charpos = index;

    char sign = 0;
      if (I < 0) {sign = '-';
    i =-I; ///More than 65536 integers, start with one of the following processing,//This processing in 100 units, that is, the remainder control in two bits//so exactly mapped to the above 10-bit and single-digit array, a one-time write//buf array of two bits, so there is no doubt than
    Out every bit is going to be a lot faster while (I >= 65536) {q = i/100;
      Really:r = i-(q * 100);R = i-((Q << 6) + (q << 5) + (Q << 2));
      i = q;
      BUF [--charpos] = digitones[r];
    BUF [--charpos] = digittens[r];
    }//Fall thru to fast mode for smaller numbers//assert (I <= 65536, i); For integers less than or equal to 65536, you can go directly to the following section//And this place is divided by 10, but the implementation is not directly in addition to//But first to find a 52429/2^19 about 0.1000 ...//I divided by 10, but I  It is not clear why this is not directly//divided by 10, perhaps because the precision is not enough, division produces floating-point numbers,//perhaps inaccurate, and then the divisor multiplied by 10, to get more than 10 bits/part of the number, through I-this part of the 10 digits, get a single-digit number for (;;)
      {q = (i * 52429) >>> (16+3); R = i-((Q << 3) + (q << 1));
      R = I (q*10) ... buf [--charpos] = digits [R];
      i = q;
    if (i = = 0) break;
    } if (sign!= 0) {buf [--charpos] = sign; final static int [] sizetable = {9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 99999999

  9, Integer.max_value}; This should be optimized by sizetable storing the bits//of integer data, from one to 10 digits: 2147483647,//This is a clever way of handling the static int stringsize (iNT X) {for (int i=0;; i++) if (x <= sizetable[i)) return i+1;

 }

Interpretation of Highestonebit method

public static int highestonebit (int i) {
    //HD, Figure 3-1
    i |= (i >> 1);
    I |= (i >> 2);
    I |= (i >> 4);
    I |= (i >> 8);
    I |= (i >>);
    Return I-(i >>> 1);
  }

This method is very interesting, I calculate, and then understand his essence, the function of this method is to form an integer of the largest bit represented by the value of the integer. This function is achieved by means of displacement. Next, for a simple example, 128来 the binary is 1000 0000. The following is an example of him:

Move 1 bit
1000 0000
0100 0000
|-------------
Move 2 bit
1100 0000
0011 0000
|------------
Move 4 bit
1111 0000
0000 1111
|------------
Move 8 bit
1111 1111
0000 0000
|------------
Move 16 bits
1111 1111
0000 0000
|------------
1111 1111

The final result, as you can see, is that all the bits in the back are filled with 1, and the rest of the bits are all cut off to get the highest number of digits represented by the integers.

Analysis of Bitcount method

public static int Bitcount (int i) {
    //HD, Figure 5-2
    i = i-((i >>> 1) & 0x55555555);
    i = (I & 0x33333333) + ((I >>> 2) & 0x33333333);
    i = (i + (i >>> 4)) & 0x0f0f0f0f;
    i = i + (i >>> 8);
    i = i + (i >>>);
    return I & 0x3f;
  }

This method has really wasted half a day of Kung Fu Research, and later to understand a probably:
The first line is to make the integer bits two two, and then count the number of 1 in the two digits, I don't know how the formula came about, but it does.
In the second line, the bits of the integral type is divided into four four groups, and then the calculation of the shift is added, which is 1001-> 10 + 01 = 11 equals three 1.
The third line is to add the bits of the integral type to the eight, and then similar to the above, and then, of course, this is done with some specific shifts and operations.
Then there are 16 groups, and 32 groups eventually merge the statistics into the last few indicated statistics.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.