Cryptography Reference
In-Depth Information
E
XAMPLE
.
Using these methods is elementary, as you can see from the following program
fragment:
BigInteger op1=new BigInteger(
“
3
”
);
BigInteger op2=new BigInteger(
“
2
”
);
BigInteger sum=op1.add(op2);
BigInteger difference=op1.subtract(op2);
BigInteger product=op1.multiply(op2);
BigInteger quotient=op1.divide(op2);
BigInteger rem=op1.remainder(op2);
•
public BigInteger[] divideAndRemainder(BigInteger val)
throws ArithmeticException
Since most division algorithms produce the quotient and the remainder at the same time,
a more efficient way of capturing both of these values is provided by the divide-
AndRemainder() method.
E
XAMPLE
.
The answers are returned in an array of two BigIntegers, as follows:
BigInteger op1=new BigInteger(
“
9
”
);
BigInteger op2=new BigInteger(
“
2
”
);
BigInteger[] answers=new BigInteger[2];
answers=op1.divideAndRemainder(op2);
When this code completes, answers[0] contains the value 4 (as a BigInteger), and
answers[1] contains 1.
•
public BigInteger pow(int exponent) throws ArithmeticException
This method returns a BigInteger whose value is
this
e
where
= exponent and throws an
e
ArithmeticException if
< 0 (as the operation would yield a noninteger value). Note that
e
e
is an integer rather than a BigInteger
E
XAMPLE
.
Here is an example of how this method would be used (it calculates 2
256
):
BigInteger base=new BigInteger(
“
2
”
);
BigInteger humungous=base.pow(256);
Clearly, care should be used with this method, for it can easily generate gigantic num-
bers which could exhaust the storage capacity of the computer.
•
public BigInteger gcd(BigInteger v)
This method returns a BigInteger whose value is the greatest common divisor of |
this
| and
|
v
|. It correctly returns (0, 0) as 0.
Search WWH ::
Custom Search