Big Integer in Java

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

lu Large Numbers in Java

 You can use the BigInteger class for integers and BigDecimal for
numbers with decimal digits. Both classes are defined in java.math
package.

Example:

BigInteger reallyBig = new BigInteger("1234567890123456890");


BigInteger notSoBig = new BigInteger("2743561234");
reallyBig = reallyBig.add(notSoBig);

BigInteger(byte[])

Translates a byte array containing the two's-complement representation of a (signed)


integer into a BigInteger.

BigInteger(int, byte[])

Translates the sign-magnitude representation of an integer into a BigInteger.

BigInteger(int, int, Random)

Returns a randomly selected BigInteger with the specified bitLength that is probably prime.

BigInteger(int, Random)

Returns a random number uniformly distributed on [0, 2**numBits - 1] (assuming a fair


source of random bits is provided in rndSrc).

BigInteger(String)

Translates a string containing an optional minus sign followed by a sequence of one or more
decimal digits into a BigInteger.

BigInteger(String, int)

Translates a string containing an optional minus sign followed by a sequence of one or more
digits in the specified radix into a BigInteger.

abs()

Returns a BigInteger whose value is the absolute value of this number.

add(BigInteger)
Returns a BigInteger whose value is (this + val).

and(BigInteger)

Returns a BigInteger whose value is (this & val).

andNot(BigInteger)

Returns a BigInteger whose value is (this & ~val).

bitCount()

Returns the number of bits in the two's complement representation of this number that
differ from its sign bit.

bitLength()

Returns the number of bits in the minimal two's-complement representation of this number,
*excluding* a sign bit, i.e., (ceil(log2(this < 0 ? -this : this + 1))).

clearBit(int)

Returns a BigInteger whose value is equivalent to this number with the designated bit
cleared.

compareTo(BigInteger)

Returns -1, 0 or 1 as this number is less than, equal to, or greater than val.

divide(BigInteger)

Returns a BigInteger whose value is (this / val).

divideAndRemainder(BigInteger)

Returns an array of two BigIntegers.

doubleValue()

Converts the number to a double.

equals(Object)

Returns true iff x is a BigInteger whose value is equal to this number.

flipBit(int)

Returns a BigInteger whose value is equivalent to this number with the designated bit
flipped.

floatValue()

Converts this number to a float.


gcd(BigInteger)

Returns a BigInteger whose value is the greatest common denominator of abs(this) and
abs(val).

getLowestSetBit()

Returns the index of the rightmost (lowest-order) one bit in this number (i.e., the number of
zero bits to the right of the rightmost one bit).

hashCode()

Computes a hash code for this object.

intValue()

Converts this number to an int.

isProbablePrime(int)

Returns true if this BigInteger is probably prime, false if it's definitely composite.

longValue()

Converts this number to a long.

max(BigInteger)

Returns the BigInteger whose value is the greater of this and val.

min(BigInteger)

Returns the BigInteger whose value is the lesser of this and val.

mod(BigInteger)

Returns a BigInteger whose value is this mod m.

modInverse(BigInteger)

Returns modular multiplicative inverse of this, mod m.

modPow(BigInteger, BigInteger)

Returns a BigInteger whose value is (this ** exponent) mod m.

multiply(BigInteger)

Returns a BigInteger whose value is (this * val).

negate()

Returns a BigInteger whose value is (-1 * this).


not()

Returns a BigInteger whose value is (~this).

or(BigInteger)

Returns a BigInteger whose value is (this | val).

pow(int)

Returns a BigInteger whose value is (this ** exponent).

remainder(BigInteger)

Returns a BigInteger whose value is (this % val).

setBit(int)

Returns a BigInteger whose value is equivalent to this number with the designated bit set.

shiftLeft(int)

Returns a BigInteger whose value is (this << n).

shiftRight(int)

Returns a BigInteger whose value is (this >> n).

signum()

Returns the signum function of this number (i.e., -1, 0 or 1 as the value of this number is
negative, zero or positive).

subtract(BigInteger)

Returns a BigInteger whose value is (this - val).

testBit(int)

Returns true iff the designated bit is set.

toByteArray()

Returns the two's-complement representation of this number.

toString()

Returns the string representation of this number, radix 10.

toString(int)

Returns the string representation of this number in the given radix.

valueOf(long)
Returns a BigInteger with the specified value.

xor(BigInteger)

Returns a BigInteger whose value is (this ^ val).

Class java.math.BigDecimal

ROUND_CEILING

If the BigDecimal is positive, behave as for ROUND_UP; if negative, behave as for


ROUND_DOWN.

ROUND_DOWN

Never increment the digit prior to a discarded fraction (i.e., truncate).

ROUND_FLOOR

If the BigDecimal is positive, behave as for ROUND_DOWN; if negative behave as for


ROUND_UP.

ROUND_HALF_DOWN

Behave as for ROUND_UP if the discarded fraction is > .5; otherwise, behave as for
ROUND_DOWN.

ROUND_HALF_EVEN

Behave as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd;
behave as for ROUND_HALF_DOWN if it's even.

ROUND_HALF_UP

Behave as for ROUND_UP if the discarded fraction is >= .5; otherwise, behave as for
ROUND_DOWN.

ROUND_UNNECESSARY

This "pseudo-rounding-mode" is actually an assertion that the requested operation has an


exact result, hence no rounding is necessary.

ROUND_UP

Always increment the digit prior to a non-zero discarded fraction.


BigDecimal(BigInteger)

Translates a BigInteger into a BigDecimal.

BigDecimal(BigInteger, int)

Translates a BigInteger and a scale into a BigDecimal.

BigDecimal(double)

Translates a double into a BigDecimal.

BigDecimal(String)

Constructs a BigDecimal from a string containing an optional minus sign followed by a


sequence of zero or more decimal digits, optionally followed by a fraction, which consists of
a decimal point followed by zero or more decimal digits.

abs()

Returns a BigDecimal whose value is the absolute value of this number, and whose scale is
this.scale().

add(BigDecimal)

Returns a BigDecimal whose value is (this + val), and whose scale is MAX(this.scale(),
val.scale).

compareTo(BigDecimal)

Returns -1, 0 or 1 as this number is less than, equal to, or greater than val.

divide(BigDecimal, int)

Returns a BigDecimal whose value is (this / val), and whose scale is this.scale().

divide(BigDecimal, int, int)

Returns a BigDecimal whose value is (this / val), and whose scale is as specified.

doubleValue()

Converts the number to a double.

equals(Object)

Returns true iff x is a BigDecimal whose value is equal to this number.

floatValue()
Converts this number to a float.

hashCode()

Computes a hash code for this object.

intValue()

Converts this number to an int.

longValue()

Converts this number to a long.

max(BigDecimal)

Returns the BigDecimal whose value is the greater of this and val.

min(BigDecimal)

Returns the BigDecimal whose value is the lesser of this and val.

movePointLeft(int)

Returns a BigDecimal which is equivalent to this one with the decimal point moved n places
to the left.

movePointRight(int)

Moves the decimal point the specified number of places to the right.

multiply(BigDecimal)

Returns a BigDecimal whose value is (this * val), and whose scale is this.scale() + val.scale.

negate()

Returns a BigDecimal whose value is -1 * this, and whose scale is this.scale().

scale()

Returns the scale of this number.

setScale(int)

Returns a BigDecimal whose scale is the specified value, and whose value is exactly equal to
this number's.

setScale(int, int)

Returns a BigDecimal whose scale is the specified value, and whose integer value is
determined by multiplying or dividing this BigDecimal's integer value by the appropriate
power of ten to maintain the overall value.
signum()

Returns the signum function of this number (i.e., -1, 0 or 1 as the value of this number is
negative, zero or positive).

subtract(BigDecimal)

Returns a BigDecimal whose value is (this - val), and whose scale is MAX(this.scale(),
val.scale).

toBigInteger()

Converts this number to a BigInteger.

toString()

Returns the string representation of this number.

valueOf(long)

Returns a BigDecimal with the given value and a scale of zero.

valueOf(long, int)

Returns a BigDecimal with a value of (val/10**scale).

1.> if (BigInteger.valueOf(intValue).compareTo(bigIntegerValue) < 0) {


// intValue is less than bigIntegerValue}

2.> The java.math.BigInteger.intValue() converts this BigInteger to an int.


3. > BigInteger.valueOF(int) converts int to Bigintger.

What is a NullPointerException, and how


do I fix it?

In this example, the variable x is an int and Java will initialize it to 0 for you. When you
assign it the value of 10 on the second line, your value of 10 is written into the memory
location referred to by x.

But, when you try to declare a reference type, something different happens. Take the
following code:

Integer num;
num = new Integer(10);

The first line declares a variable named num, but it does not actually contain a primitive value
yet. Instead, it contains a pointer (because the type is Integer which is a reference type).
Since you have not yet said what to point to, Java sets it to null, which means "I am
pointing to nothing".

In the second line, the new keyword is used to instantiate (or create) an object of type
Integer and the pointer variable num is assigned to that Integer object.

The NullPointerException occurs when you declare a variable but did not create an object.
So you are pointing to something that does not actually exist.

You might also like