Java – how to operate on large numbers that cannot be stored in a variable
•
Java
In Java, I want to be able to operate on very large integers (which cannot be stored for a long time). What should I do?
What is the best way to do well? Should I create my own data type with several long variables?
Example:
public class MyBigInteger{
private long firstPart;
private long secondPart;
...
}
public MyBigInteger add(long a,long b){
MyBigInteger res;
// WHAT CAN I DO HERE,I guess I Could do something with the >> << operators,but I've never used them!
return res;
}
thank you!
Solution
import java.math.BigInteger;
import java.math.BigInteger;
public class BigIntegerTest {
public BigInteger add(long a,long b){
BigInteger big1 = new BigInteger(Long.toString(a));
BigInteger big2 = new BigInteger(Long.toString(b));
return big1.add(big2);
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new BigIntegertest().add(22342342424323423L,234234234234234234L));
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
