Today reference textbook wrote a program about binary and decimal conversion, the program algorithm is not difficult, but after the test found that whether it is two to ten or ten to two, for greater than 2.1 billion is more than the integer range of the number can not be very good conversion. will become 0.
Reference books found using BigInteger can solve this problem.
Then found the next JDK, and then test a few times finally write success!
The following tips are used:
1,biginteger belongs to Java.math.BigInteger, so import this class before each use. I forgot import at first, so I always hint that I can't find the prompt.
2, there are many ways to construct it, but now I use the following:
biginteger Span style= "Font-family:nsimsun;" > ( string val) converts the BigInteger decimal string representation to BigInteger. |
biginteger< /span> string Span style= "Font-family:nsimsun;" > val, int radix) Converts the string representation of the BigInteger of the specified cardinality to BigInteger. |
If you want to convert the Int type 2 to BigInteger type, write as BigInteger two=new BigInteger ("2"); Note 2 double quotes cannot be omitted
The 3,biginteger class simulates all types of int math operations, such as add () = = "+", divide () = = "-", etc., but note that its contents cannot be manipulated directly using mathematical operators, but must use its internal methods. And its operands must also be of type BigInteger.
such as: Two.add (2) is a wrong operation, because 2 has not changed into a BigInteger type.
4, you should use the. ToString method to convert the results of the calculation to a 10-binary string, as described in detail below:
String |
toString() Returns the decimal string representation of this BigInteger. |
output method: System.out.print (Two.tostring ());
5, plus three functions used.
biginteger |
remainder ( biginteger val) returns a value of &NBSP; (this% val) BigInteger. |
biginteger |
negate return its value is &NBSP; (-this) 's BigInteger. |
int |
compareTo(BigInteger val) 将此 BigInteger 与指定的 BigInteger 进行比较。 |
Remainder is used to find the remainder.
Negate changes the operand to the opposite number.
Compare's explanation is as follows:
CompareTo
CompareTo (BigInteger val)
-
compares this BigInteger with the specified BigInteger. This method was provided in preference to individual methods for each of the six Boolean comparison operators (<, = =, &G t;, >=,! =, <=). The suggested idiom for performing these comparisons are: (X.compareto (y) <
op > 0) , where <
op > is one of the six comparison operators.
-
-
Specified by:
-
compareTo
in interface
comparable<biginteger>
-
-
Parameters:
-
Val
-BigInteger to which this BigInteger was to be compared.
-
Returns:
-
-1, 0 or 1 as this BigInteger are numerically less than, E Qual to, or greater than Val .
ImportJava.math.BigInteger; Public classBigintegerdemo { Public Static voidMain (string[] args) {BigInteger big=Biginteger.one; System.out.println ("Biginteger.one:" +big); System.out.println ("Nextprobableprime:" +big.nextprobableprime ()); System.out.println ("Nextprobableprime:" +big.nextprobableprime ()); Big=Biginteger.ten; System.out.println ("Biginteger.ten:" +big); Big=Biginteger.zero; System.out.println ("Biginteger.zero:" +big); }}
View Code
Output:
Biginteger.one:1nextprobableprime:2nextprobableprime:2biginteger.ten: Biginteger.zero:0