The source code and analysis of BigInteger large number home law

Source: Internet
Author: User

  1. Public BigInteger (String val, int radix) {
  2. int cursor = 0, numdigits;
  3. int len = Val.length (); //Gets the length of the string
  4. //Non-qualifying conditions
  5. if (Radix < Character.min_radix | | radix > Character.max_radix)
  6. throw New NumberFormatException ("Radix out of Range");
  7. if (val.length () = = 0)
  8. throw New NumberFormatException ("Zero length BigInteger");
  9. //positive or negative, handle the "-" inside the string
  10. Signum = 1;
  11. int index = val.lastindexof ("-");
  12. if (Index! =-1) {
  13. if (index = = 0) {
  14. if (val.length () = = 1)
  15. throw New NumberFormatException ("Zero length BigInteger");
  16. Signum =-1;
  17. cursor = 1;
  18. } Else {
  19. throw New NumberFormatException ("illegal embedded minus sign");
  20. }
  21. }
  22. //Skip the previous 0
  23. While (cursor < len &&
  24. Character.digit (Val.charat (cursor), radix) = = 0)
  25. cursor++;
  26. if (cursor = = len) {//If the string is all 0, it is stored as Zero.mag
  27. Signum = 0;
  28. Mag = Zero.mag;
  29. return;
  30. } Else {//numdigits is actually a valid number
  31. Numdigits = Len-cursor;
  32. }
  33. how many bits will be required to convert the radix number of//numdigits bits into 2 binary
  34. The elements in the//bitsperdigit array are multiplied by 1024 and need to be shifted right 10 bits (equivalent to dividing by 1024), when doing division there will be
  35. //decimal loss, so add 1 to ensure that the number of digits must be sufficient
  36. //An int has 32bit, so dividing by 32 is the size of the mag array we started estimating
  37. int numbits = (int) (((Numdigits * Bitsperdigit[radix]) >>> ) + 1);
  38. int numwords = (numbits + + )/32;
  39. Mag = new int[numwords];
  40. //start to intercept the number in the string according to Digitsperint
  41. //Will not be enough Digitsperint[radix] first take out the conversion
  42. int firstgrouplen = numdigits% digitsperint[radix];
  43. if (Firstgrouplen = = 0)
  44. Firstgrouplen = Digitsperint[radix];
  45. //Put the number of the first paragraph in the last digit of the mag array
  46. String Group = val.substring (cursor, cursor + = Firstgrouplen);
  47. Mag[mag.length- 1] = Integer.parseint (group, Radix);
  48. if (Mag[mag.length- 1] < 0)
  49. throw New NumberFormatException ("illegal digit");
  50. //The remaining segment conversion
  51. int superradix = Intradix[radix];
  52. int groupval = 0;
  53. While (Cursor < val.length ()) {
  54. Group = val.substring (cursor, cursor + + Digitsperint[radix]);
  55. Groupval = Integer.parseint (group, Radix);
  56. if (Groupval < 0)
  57. throw New NumberFormatException ("illegal digit");
  58. Destructivemuladd (Mag, Superradix, groupval);
  59. }
  60. Mag = trustedstripleadingzeroints (MAG);
  61. }

The source code and analysis of BigInteger large number home law

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.