java--learning the integer source (slowly fill)

Source: Internet
Author: User

Do not look at spring today, on the day of the class, the evening also to write compiling principle work (headache). First look at the API source code to relax. I have to say, seeing what the world's top programmers have written is just as enjoyable as watching the NBA, though a bit burning.

Let's see how integer.tostring (int i) is implemented.

First there are two related methods, stringsize (int x) and getchars (int i, int index, char[] buf).

 1  final  static  int  [] sizetable = {9,99,999,9999,99999,999999,9999999,99999999,999999999,integer.max_ VALUE};  2  static   int  stringsize (int   x) { 3  for  (int  i=0;; i+ +)  4  if  (X<= Sizetable[i])  5  return  i+1;  6 } 

It turns out that the number of digits of a number is so fun, it's so hard to cry, and suddenly I think of my past 10, almost flushed. Can't help copying it again.

1 Static voidGetChars (intIintIndexChar[] buf) {    2     intQ,r;3     intCharpos =index;4     CharSign =0;5     if(I < 0) {    6Sign = '-';7i =-1;8     }9      while(I >= 65536) {TenQ = i/100; OneR = I (q << 6) + (q << 5) + (q << 2); AI=Q; -BUF [--charpos] =Digitones[r]; -BUF [--charpos] =Digittens[r]; the     } - 

Continue to copy, say the code to this side I can still accept, each time take the first two digits converted into Char put into the BUF array. But I saw the next section I was going to collapse. This is how the trough, why can write the code so elegant ...

 // fall thru to fast mode for Smaler numbers  // assert (i <=65536, i);  for   (;;)    {Q  = (i *52429) >>> (16+3 = i-((Q << 3) + (q << 1--charpos] = digits [R];    I  = Q;  if  (i = = 0) break  "}  if  (sign! = 0-    -charpos] = sign; }}

Simply handsome fried good, finally know why the front to use 65536 demarcation. Because 65536*52429 will cross the border. Q = (i*52429) >>> (16+3) is equivalent to i*52429/524288. 52429/524288=0.1000004, this is the mother of the speed of the program to the extreme. Writing code is such an interesting thing. Cover your face. jpg.

1  Public StaticString toString (inti) {2    if(i = =integer.min_value)3         return"-2147483648";4    intSize = (i<0)? Stringsize (-i) + 1: Stringsize (i);5    Char[] buf =New Char[size];6 GetChars (i, size, buf);7    return NewString (BUF,true);8}

ToString itself is very well understood, the first if mainly because the range of the int negative number is larger than the number of positive, improper exception list, it is difficult to determine the array of bits. The next negative number of digits plus one is because negative numbers are signed-.

Reasoning today also saw a parseint implementation, mainly by calling Character.digit (). To write homework, too lazy to write. To be continued, slowly fill the whole ...

java--learning the integer source (slowly fill)

Related Article

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.