Class EInteger
- java.lang.Object
-
- com.upokecenter.numbers.EInteger
-
- All Implemented Interfaces:
java.lang.Comparable<EInteger>
public final class EInteger extends java.lang.Object implements java.lang.Comparable<EInteger>
Represents an arbitrary-precision integer. (The "E" stands for "extended", and has this prefix to group it with the other classes common to this library, particularly EDecimal, EFloat, and ERational.)Instances of this class are immutable, so they are inherently safe for use by multiple threads. Multiple instances of this object with the same value are interchangeable, but they should be compared using the "Equals" method rather than the "==" operator.
Security note
It is not recommended to implement security-sensitive algorithms using the methods in this class, for several reasons:
EIntegerobjects are immutable, so they can't be modified, and the memory they occupy is not guaranteed to be cleared in a timely fashion due to garbage collection. This is relevant for applications that use many-bit-long numbers as secret parameters.- The methods in this class (especially those that involve arithmetic) are not guaranteed to be "constant-time" (non-data-dependent) for all relevant inputs. Certain attacks that involve encrypted communications have exploited the timing and other aspects of such communications to derive keying material or cleartext indirectly.
Applications should instead use dedicated security libraries to handle big numbers in security-sensitive algorithms.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description EIntegerAbs()Returns the absolute value of this object's value.EIntegerAdd(int intValue)Adds this object and another object.EIntegerAdd(EInteger bigintAugend)Adds this object and another object.EIntegerAnd(EInteger other)Does an AND operation between two arbitrary-precision integer values.intAsInt32Checked()Deprecated.Renamed to ToInt32Checked.intAsInt32Unchecked()Deprecated.Renamed to ToInt32Unchecked.longAsInt64Checked()Deprecated.Renamed to ToInt64Checked.longAsInt64Unchecked()Deprecated.Renamed to ToInt64Unchecked.booleanCanFitInInt32()Returns whether this object's value can fit in a 32-bit signed integer.booleanCanFitInInt64()Returns whether this object's value can fit in a 64-bit signed integer.intcompareTo(int intValue)Compares an arbitrary-precision integer with this instance.intcompareTo(EInteger other)Compares an arbitrary-precision integer with this instance.EIntegerDivide(int intValue)Divides this instance by the value of an arbitrary-precision integer.EIntegerDivide(EInteger bigintDivisor)Divides this instance by the value of an arbitrary-precision integer.EInteger[]DivRem(EInteger divisor)Divides this object by another arbitrary-precision integer and returns the quotient and remainder.booleanequals(java.lang.Object obj)Determines whether this object and another object are equal and have the same type.static EIntegerFromBoolean(boolean boolValue)Converts a boolean value (true or false) to an arbitrary-precision integer.static EIntegerFromByte(byte inputByte)Converts a byte (from 0 to 255) to an arbitrary-precision integer.static EIntegerFromBytes(byte[] bytes, boolean littleEndian)Initializes an arbitrary-precision integer from an array of bytes.static EIntegerFromInt16(short inputInt16)Converts a 16-bit signed integer to an arbitrary-precision integer.static EIntegerFromInt32(int intValue)Converts a 32-bit signed integer to an arbitrary-precision integer.static EIntegerFromInt64(long longerValue)Converts a 64-bit signed integer to an arbitrary-precision integer.static EIntegerFromRadixString(java.lang.String str, int radix)Converts a string to an arbitrary-precision integer in a given radix.static EIntegerFromRadixSubstring(java.lang.String str, int radix, int index, int endIndex)Converts a portion of a string to an arbitrary-precision integer in a given radix.static EIntegerFromString(java.lang.String str)Converts a string to an arbitrary-precision integer.static EIntegerFromSubstring(java.lang.String str, int index, int endIndex)Converts a portion of a string to an arbitrary-precision integer.EIntegerGcd(EInteger bigintSecond)Returns the greatest common divisor of this integer and the given integer.intGetDigitCount()Deprecated.This method may overflow.EIntegerGetDigitCountAsEInteger()Returns the number of decimal digits used by this integer, in the form of an arbitrary-precision integer.intGetLowBit()Deprecated.This method may overflow.EIntegerGetLowBitAsEInteger()Gets the lowest set bit in this number's absolute value, in the form of an arbitrary-precision integer.static EIntegergetOne()Gets the number 1 as an arbitrary-precision integer.booleanGetSignedBit(int index)Returns whether a bit is set in the two's-complement form (see"Forms of numbers") of this object' s value.booleanGetSignedBit(EInteger bigIndex)Returns whether a bit is set in the two's-complement form (see"Forms of numbers") of this object' s value.intGetSignedBitLength()Deprecated.This method may overflow.EIntegerGetSignedBitLengthAsEInteger()Finds the minimum number of bits needed to represent this object's value, except for its sign, in the form of an arbitrary-precision integer.static EIntegergetTen()Gets the number 10 as an arbitrary-precision integer.booleanGetUnsignedBit(int index)Returns whether a bit is set in this number's absolute value.booleanGetUnsignedBit(EInteger bigIndex)Returns whether a bit is set in this number's absolute value.intGetUnsignedBitLength()Deprecated.This method may overflow.EIntegerGetUnsignedBitLengthAsEInteger()Finds the minimum number of bits needed to represent this number's absolute value.static EIntegergetZero()Gets the number zero as an arbitrary-precision integer.inthashCode()Returns the hash code for this instance.booleanisEven()Gets a value indicating whether this value is even.booleanisPowerOfTwo()Gets a value indicating whether this object's value is a power of two, and greater than 0.booleanisZero()Gets a value indicating whether this value is 0.EIntegerMod(int smallDivisor)Finds the modulus remainder that results when this instance is divided by the value of another integer.EIntegerMod(EInteger divisor)Finds the modulus remainder that results when this instance is divided by the value of an arbitrary-precision integer.EIntegerModPow(EInteger pow, EInteger mod)Calculates the remainder when this arbitrary-precision integer raised to a certain power is divided by another arbitrary-precision integer.EIntegerMultiply(int intValue)Multiplies this instance by the value of an arbitrary-precision integer object.EIntegerMultiply(EInteger bigintMult)Multiplies this instance by the value of an arbitrary-precision integer object.EIntegerNegate()Gets the value of this object with the sign reversed.EIntegerNot()Returns an arbitrary-precision integer with every bit flipped from this one.EIntegerOr(EInteger second)Does an OR operation between two arbitrary-precision integer instances.EIntegerPow(int powerSmall)Raises an arbitrary-precision integer to a power.EIntegerPow(EInteger bigPower)Raises an arbitrary-precision integer to a power.EIntegerPowBigIntVar(EInteger power)Raises an arbitrary-precision integer to a power, which is given as another arbitrary-precision integer.EIntegerRemainder(int intValue)Finds the remainder that results when this instance is divided by the value of an arbitrary-precision integer.EIntegerRemainder(EInteger divisor)Finds the remainder that results when this instance is divided by the value of an arbitrary-precision integer.EIntegerShiftLeft(int numberBits)Returns an arbitrary-precision integer with the bits shifted to the left by a number of bits.EIntegerShiftLeft(EInteger eshift)Returns an arbitrary-precision integer with the bits shifted to the left by a number of bits given as an arbitrary-precision integer.EIntegerShiftRight(int numberBits)Returns an arbitrary-precision integer with the bits shifted to the right.EIntegerShiftRight(EInteger eshift)Returns an arbitrary-precision integer with the bits shifted to the right.intsignum()Gets the sign of this object's value.EIntegerSqrt()Finds the square root of this instance's value, rounded down.EInteger[]SqrtRem()Calculates the square root and the remainder.EIntegerSubtract(int intValue)Subtracts an arbitrary-precision integer from this arbitrary-precision integer.EIntegerSubtract(EInteger subtrahend)Subtracts an arbitrary-precision integer from this arbitrary-precision integer.byteToByteChecked()Converts this number's value to a byte (from 0 to 255) if it can fit in a byte (from 0 to 255).byte[]ToBytes(boolean littleEndian)Returns a byte array of this integer's value.byteToByteUnchecked()Converts this number to a byte (from 0 to 255), returning the least-significant bits of this number's two's-complement form.shortToInt16Checked()Converts this number's value to a 16-bit signed integer if it can fit in a 16-bit signed integer.shortToInt16Unchecked()Converts this number to a 16-bit signed integer, returning the least-significant bits of this number's two's-complement form.intToInt32Checked()Converts this object's value to a 32-bit signed integer, throwing an exception if it can't fit.intToInt32Unchecked()Converts this object's value to a 32-bit signed integer.longToInt64Checked()Converts this object's value to a 64-bit signed integer, throwing an exception if it can't fit.longToInt64Unchecked()Converts this object's value to a 64-bit signed integer.java.lang.StringToRadixString(int radix)Generates a string representing the value of this object, in the given radix.java.lang.StringtoString()Converts this object to a text string in base 10.EIntegerXor(EInteger other)Finds the exclusive "or" of two arbitrary-precision integer objects.
-
-
-
Method Detail
-
getOne
public static EInteger getOne()
Gets the number 1 as an arbitrary-precision integer.- Returns:
- The number 1 as an arbitrary-precision integer.
-
getTen
public static EInteger getTen()
Gets the number 10 as an arbitrary-precision integer.- Returns:
- The number 10 as an arbitrary-precision integer.
-
getZero
public static EInteger getZero()
Gets the number zero as an arbitrary-precision integer.- Returns:
- The number zero as an arbitrary-precision integer.
-
isEven
public final boolean isEven()
Gets a value indicating whether this value is even.- Returns:
trueif this value is even; otherwise,false.
-
isPowerOfTwo
public final boolean isPowerOfTwo()
Gets a value indicating whether this object's value is a power of two, and greater than 0.- Returns:
trueif this object' s value is a power of two, and greater than 0; otherwise,false.
-
isZero
public final boolean isZero()
Gets a value indicating whether this value is 0.- Returns:
trueif this value is 0; otherwise,false.
-
signum
public final int signum()
Gets the sign of this object's value.- Returns:
- The sign of this object' s value.
-
FromBytes
public static EInteger FromBytes(byte[] bytes, boolean littleEndian)
Initializes an arbitrary-precision integer from an array of bytes.- Parameters:
bytes- A byte array consisting of the two's-complement form (see"Forms of numbers") of the arbitrary-precision integer to create. The byte array is encoded using the following rules:- Positive numbers have the first byte's highest bit cleared, and negative numbers have the bit set.
- The last byte contains the lowest 8-bits, the next-to-last
contains the next lowest 8 bits, and so on. For example, the number
300 can be encoded as
0x01, 0x2cand 200 as0x00, 0xc8. (Note that the second example contains a set high bit in0xc8, so an additional 0 is added at the start to ensure it's interpreted as positive.) - To encode negative numbers,
take the absolute value of the number, subtract by 1, encode the
number into bytes, and toggle each bit of each byte. Any further bits
that appear beyond the most significant bit of the number will be all
ones. For example, the number -450 can be encoded as
0xfe, 0x70and -52869 as0xff, 0x31, 0x7b. (Note that the second example contains a cleared high bit in0x31, 0x7b, so an additional 0xff is added at the start to ensure it's interpreted as negative.)
For little-endian, the byte order is reversed from the byte order just discussed.
.littleEndian- If true, the byte order is little-endian, or least-significant-byte first. If false, the byte order is big-endian, or most-significant-byte first.- Returns:
- An arbitrary-precision integer. Returns 0 if the byte array's length is 0.
- Throws:
java.lang.NullPointerException- The parameterbytesis null.
-
FromBoolean
public static EInteger FromBoolean(boolean boolValue)
Converts a boolean value (true or false) to an arbitrary-precision integer.- Parameters:
boolValue- Either true or false.- Returns:
- The number 1 if
boolValueis true; otherwise, 0.
-
FromInt32
public static EInteger FromInt32(int intValue)
Converts a 32-bit signed integer to an arbitrary-precision integer.- Parameters:
intValue- The parameterintValueis a 32-bit signed integer.- Returns:
- An arbitrary-precision integer with the same value as the 64-bit number.
-
FromInt64
public static EInteger FromInt64(long longerValue)
Converts a 64-bit signed integer to an arbitrary-precision integer.- Parameters:
longerValue- The parameterlongerValueis a 64-bit signed integer.- Returns:
- An arbitrary-precision integer with the same value as the 64-bit number.
-
FromRadixString
public static EInteger FromRadixString(java.lang.String str, int radix)
Converts a string to an arbitrary-precision integer in a given radix.- Parameters:
str- A string described by the FromRadixSubstring method.radix- A base from 2 to 36. Depending on the radix, the string can use the basic digits 0 to 9 (U + 0030 to U + 0039) and then the basic letters A to Z (U + 0041 to U + 005A). For example, 0-9 in radix 10, and 0-9, then A-F in radix 16.- Returns:
- An arbitrary-precision integer with the same value as the given string.
- Throws:
java.lang.NullPointerException- The parameterstris null.
-
FromRadixSubstring
public static EInteger FromRadixSubstring(java.lang.String str, int radix, int index, int endIndex)
Converts a portion of a string to an arbitrary-precision integer in a given radix.- Parameters:
str- A text string. The desired portion of the string must contain only characters allowed by the given radix, except that it may start with a minus sign ("-", U+002D) to indicate a negative number. The desired portion is not allowed to contain white space characters, including spaces.radix- A base from 2 to 36. Depending on the radix, the string can use the basic digits 0 to 9 (U + 0030 to U + 0039) and then the basic letters A to Z (U + 0041 to U + 005A). For example, 0-9 in radix 10, and 0-9, then A-F in radix 16.index- The index of the string that starts the string portion.endIndex- The index of the string that ends the string portion. The length will be index + endIndex - 1.- Returns:
- An arbitrary-precision integer with the same value as given in the string portion.
- Throws:
java.lang.NullPointerException- The parameterstris null.java.lang.NumberFormatException- The string portion is empty or in an invalid format.java.lang.IllegalArgumentException- Doesn't satisfy (endIndex - index) % 4 == 0".
-
FromString
public static EInteger FromString(java.lang.String str)
Converts a string to an arbitrary-precision integer.- Parameters:
str- A text string. The string must contain only basic digits 0 to 9 (U+0030 to U+0039), except that it may start with a minus sign ("-", U + 002D) to indicate a negative number. The string is not allowed to contain white space characters, including spaces.- Returns:
- An arbitrary-precision integer with the same value as given in the string.
- Throws:
java.lang.NumberFormatException- The parameterstris in an invalid format.java.lang.NullPointerException- The parameterstris null.
-
FromSubstring
public static EInteger FromSubstring(java.lang.String str, int index, int endIndex)
Converts a portion of a string to an arbitrary-precision integer.- Parameters:
str- A text string. The desired portion of the string must contain only basic digits 0 to 9 (U + 0030 to U + 0039), except that it may start with a minus sign ("-", U+002D) to indicate a negative number. The desired portion is not allowed to contain white space characters, including spaces.index- The index of the string that starts the string portion.endIndex- The index of the string that ends the string portion. The length will be index + endIndex - 1.- Returns:
- An arbitrary-precision integer with the same value as given in the string portion.
- Throws:
java.lang.IllegalArgumentException- The parameterindexis less than 0,endIndexis less than 0, or either is greater than the string's length, orendIndexis less thanindex.java.lang.NullPointerException- The parameterstris null.
-
Abs
public EInteger Abs()
Returns the absolute value of this object's value.- Returns:
- This object's value with the sign removed.
-
Add
public EInteger Add(EInteger bigintAugend)
Adds this object and another object.- Parameters:
bigintAugend- Another arbitrary-precision integer.- Returns:
- The sum of the two objects.
- Throws:
java.lang.NullPointerException- The parameterbigintAugendis null.
-
AsInt32Checked
@Deprecated public int AsInt32Checked()
Deprecated.Renamed to ToInt32Checked.Converts this object's value to a 32-bit signed integer, throwing an exception if it can't fit.- Returns:
- A 32-bit signed integer.
-
AsInt32Unchecked
@Deprecated public int AsInt32Unchecked()
Deprecated.Renamed to ToInt32Unchecked.Converts this object's value to a 32-bit signed integer. If the value can't fit in a 32-bit integer, returns the lower 32 bits of this object's two' s-complement form (see"Forms of numbers") (in which case the return value might have a different sign than this object's value).- Returns:
- A 32-bit signed integer.
-
AsInt64Checked
@Deprecated public long AsInt64Checked()
Deprecated.Renamed to ToInt64Checked.Converts this object's value to a 64-bit signed integer, throwing an exception if it can't fit.- Returns:
- A 64-bit signed integer.
-
AsInt64Unchecked
@Deprecated public long AsInt64Unchecked()
Deprecated.Renamed to ToInt64Unchecked.Converts this object's value to a 64-bit signed integer. If the value can't fit in a 64-bit integer, returns the lower 64 bits of this object's two' s-complement form (see"Forms of numbers") (in which case the return value might have a different sign than this object's value).- Returns:
- A 64-bit signed integer.
-
CanFitInInt32
public boolean CanFitInInt32()
Returns whether this object's value can fit in a 32-bit signed integer.- Returns:
trueif this object's value is from -2147483648 through 2147483647; otherwise,false.
-
CanFitInInt64
public boolean CanFitInInt64()
Returns whether this object's value can fit in a 64-bit signed integer.- Returns:
trueif this object's value is from -9223372036854775808 through 9223372036854775807; otherwise,false.
-
compareTo
public int compareTo(EInteger other)
Compares an arbitrary-precision integer with this instance.- Specified by:
compareToin interfacejava.lang.Comparable<EInteger>- Parameters:
other- The integer to compare to this value.- Returns:
- Zero if the values are equal; a negative number if this instance is less, or a positive number if this instance is greater.
-
Add
public EInteger Add(int intValue)
Adds this object and another object.EInteger result = EInteger.FromString("5").Add(200);.- Parameters:
intValue- The parameterintValueis a 32-bit signed integer.- Returns:
- The sum of the two objects.
-
Subtract
public EInteger Subtract(int intValue)
Subtracts an arbitrary-precision integer from this arbitrary-precision integer.- Parameters:
intValue- The parameterintValueis a 32-bit signed integer.- Returns:
- The difference of the two objects.
-
Multiply
public EInteger Multiply(int intValue)
Multiplies this instance by the value of an arbitrary-precision integer object.EInteger result = EInteger.FromString("5").Multiply(200);.- Parameters:
intValue- The parameterintValueis a 32-bit signed integer.- Returns:
- The product of the two numbers.
-
Divide
public EInteger Divide(int intValue)
Divides this instance by the value of an arbitrary-precision integer. The result is rounded down (the fractional part is discarded). Except if the result is 0, it will be negative if this object is positive and the other is negative, or vice versa, and will be positive if both are positive or both are negative.- Parameters:
intValue- The divisor.- Returns:
- The quotient of the two objects.
- Throws:
java.lang.ArithmeticException- Attempted to divide by zero.
-
Remainder
public EInteger Remainder(int intValue)
Finds the remainder that results when this instance is divided by the value of an arbitrary-precision integer. The remainder is the value that remains when the absolute value of this object is divided by the absolute value of the other object; the remainder has the same sign (positive or negative) as this object.- Parameters:
intValue- The parameterintValueis a 32-bit signed integer.- Returns:
- The remainder of the two numbers.
- Throws:
java.lang.ArithmeticException- Attempted to divide by zero.java.lang.NullPointerException- The parameterintValueis null.
-
compareTo
public int compareTo(int intValue)
Compares an arbitrary-precision integer with this instance.- Parameters:
intValue- The parameterintValueis a 32-bit signed integer.- Returns:
- Zero if the values are equal; a negative number if this instance is less, or a positive number if this instance is greater.
-
Divide
public EInteger Divide(EInteger bigintDivisor)
Divides this instance by the value of an arbitrary-precision integer. The result is rounded down (the fractional part is discarded). Except if the result is 0, it will be negative if this object is positive and the other is negative, or vice versa, and will be positive if both are positive or both are negative.- Parameters:
bigintDivisor- The divisor.- Returns:
- The quotient of the two objects.
- Throws:
java.lang.NullPointerException- The parameterbigintDivisoris null.java.lang.ArithmeticException- Attempted to divide by zero.
-
DivRem
public EInteger[] DivRem(EInteger divisor)
Divides this object by another arbitrary-precision integer and returns the quotient and remainder.- Parameters:
divisor- The number to divide by.- Returns:
- An array with two arbitrary-precision integers: the first is the quotient, and the second is the remainder.
- Throws:
java.lang.ArithmeticException- The parameter divisor is 0.java.lang.NullPointerException- The parameterdivisoris null.
-
equals
public boolean equals(java.lang.Object obj)
Determines whether this object and another object are equal and have the same type.- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- The parameterobjis an arbitrary object.- Returns:
trueif this object and another object are equal; otherwise,false.
-
Gcd
public EInteger Gcd(EInteger bigintSecond)
Returns the greatest common divisor of this integer and the given integer. The greatest common divisor (GCD) is also known as the greatest common factor (GCF).- Parameters:
bigintSecond- Another arbitrary-precision integer.- Returns:
- An arbitrary-precision integer.
- Throws:
java.lang.NullPointerException- The parameterbigintSecondis null.
-
GetDigitCountAsEInteger
public EInteger GetDigitCountAsEInteger()
Returns the number of decimal digits used by this integer, in the form of an arbitrary-precision integer.- Returns:
- The number of digits in the decimal form of this integer. Returns 1 if this number is 0.
-
GetDigitCount
@Deprecated public int GetDigitCount()
Deprecated.This method may overflow. Use GetDigitCountAsEInteger instead.Returns the number of decimal digits used by this integer.- Returns:
- The number of digits in the decimal form of this integer. Returns 1 if this number is 0.
- Throws:
java.lang.ArithmeticException- The return value would exceed the range of a 32-bit signed integer.
-
hashCode
public int hashCode()
Returns the hash code for this instance. No application or process IDs are used in the hash code calculation.- Overrides:
hashCodein classjava.lang.Object- Returns:
- A 32-bit signed integer.
-
GetLowBit
@Deprecated public int GetLowBit()
Deprecated.This method may overflow. Use GetLowBitAsEInteger instead.Gets the lowest set bit in this number's absolute value. (This will also be the lowest set bit in the number's two's-complement form (see"Forms of numbers").).- Returns:
- The lowest bit set in the number, starting at 0. Returns -1 if this value is 0.
-
GetLowBitAsEInteger
public EInteger GetLowBitAsEInteger()
Gets the lowest set bit in this number's absolute value, in the form of an arbitrary-precision integer. (This will also be the lowest set bit in the number's two's-complement form (see"Forms of numbers").).- Returns:
- The lowest bit set in the number, starting at 0. Returns -1 if this value is 0 or odd.
-
GetSignedBit
public boolean GetSignedBit(EInteger bigIndex)
Returns whether a bit is set in the two's-complement form (see"Forms of numbers") of this object' s value.- Parameters:
bigIndex- An arbitrary-precision integer.- Returns:
trueif a bit is set in the two' s-complement form (seeEDecimal) of this object' s value; otherwise,false.- Throws:
java.lang.NullPointerException- The parameterbigIndexis null.
-
GetSignedBit
public boolean GetSignedBit(int index)
Returns whether a bit is set in the two's-complement form (see"Forms of numbers") of this object' s value.- Parameters:
index- The parameterindexis a 32-bit signed integer.- Returns:
trueif a bit is set in the two' s-complement form (seeEDecimal) of this object' s value; otherwise,false.
-
GetSignedBitLengthAsEInteger
public EInteger GetSignedBitLengthAsEInteger()
Finds the minimum number of bits needed to represent this object's value, except for its sign, in the form of an arbitrary-precision integer. If the value is negative, finds the number of bits in the value equal to this object's absolute value minus 1.- Returns:
- The number of bits in this object's value. Returns 0 if this object's value is 0 or negative 1.
-
GetSignedBitLength
@Deprecated public int GetSignedBitLength()
Deprecated.This method may overflow. Use GetSignedBitLengthAsEInteger instead.Finds the minimum number of bits needed to represent this object's value, except for its sign. If the value is negative, finds the number of bits in the value equal to this object's absolute value minus 1.- Returns:
- The number of bits in this object's value. Returns 0 if this object's value is 0 or negative 1.
- Throws:
java.lang.ArithmeticException- The return value would exceed the range of a 32-bit signed integer.
-
GetUnsignedBit
public boolean GetUnsignedBit(EInteger bigIndex)
Returns whether a bit is set in this number's absolute value.- Parameters:
bigIndex- An arbitrary-precision integer.- Returns:
trueif a bit is set in this number's absolute value.- Throws:
java.lang.NullPointerException- The parameterbigIndexis null.
-
GetUnsignedBit
public boolean GetUnsignedBit(int index)
Returns whether a bit is set in this number's absolute value.- Parameters:
index- Zero based index of the bit to test. 0 means the least significant bit.- Returns:
trueif a bit is set in this number's absolute value.
-
GetUnsignedBitLengthAsEInteger
public EInteger GetUnsignedBitLengthAsEInteger()
Finds the minimum number of bits needed to represent this number's absolute value.- Returns:
- The number of bits in this object's value. Returns 0 if this object's value is 0, and returns 1 if the value is negative 1.
-
GetUnsignedBitLength
@Deprecated public int GetUnsignedBitLength()
Deprecated.This method may overflow. Use GetUnsignedBitLengthAsEInteger instead.Finds the minimum number of bits needed to represent this number's absolute value.- Returns:
- The number of bits in this object's value. Returns 0 if this object's value is 0, and returns 1 if the value is negative 1.
- Throws:
java.lang.ArithmeticException- The return value would exceed the range of a 32-bit signed integer.
-
Mod
public EInteger Mod(EInteger divisor)
Finds the modulus remainder that results when this instance is divided by the value of an arbitrary-precision integer. The modulus remainder is the same as the normal remainder if the normal remainder is positive, and equals divisor plus normal remainder if the normal remainder is negative.- Parameters:
divisor- The number to divide by.- Returns:
- An arbitrary-precision integer.
- Throws:
java.lang.IllegalArgumentException- The parameterdivisoris less than 0.java.lang.NullPointerException- The parameterdivisoris null.
-
Mod
public EInteger Mod(int smallDivisor)
Finds the modulus remainder that results when this instance is divided by the value of another integer. The modulus remainder is the same as the normal remainder if the normal remainder is positive, and equals divisor plus normal remainder if the normal remainder is negative.- Parameters:
smallDivisor- The divisor of the modulus.- Returns:
- The modulus remainder.
- Throws:
java.lang.IllegalArgumentException- The parametersmallDivisoris less than 0.
-
ModPow
public EInteger ModPow(EInteger pow, EInteger mod)
Calculates the remainder when this arbitrary-precision integer raised to a certain power is divided by another arbitrary-precision integer.- Parameters:
pow- The power to raise this integer by.mod- The integer to divide the raised number by.- Returns:
- An arbitrary-precision integer.
- Throws:
java.lang.NullPointerException- The parameterpowormodis null.
-
Multiply
public EInteger Multiply(EInteger bigintMult)
Multiplies this instance by the value of an arbitrary-precision integer object.- Parameters:
bigintMult- Another arbitrary-precision integer.- Returns:
- The product of the two numbers.
- Throws:
java.lang.NullPointerException- The parameterbigintMultis null.
-
Negate
public EInteger Negate()
Gets the value of this object with the sign reversed.- Returns:
- This object's value with the sign reversed.
-
Pow
public EInteger Pow(EInteger bigPower)
Raises an arbitrary-precision integer to a power.- Parameters:
bigPower- The exponent to raise this integer to.- Returns:
- The result. Returns 1 if
bigPoweris 0. - Throws:
java.lang.NullPointerException- The parameterbigPoweris null.java.lang.IllegalArgumentException- BigPower is negative.
-
Pow
public EInteger Pow(int powerSmall)
Raises an arbitrary-precision integer to a power.- Parameters:
powerSmall- The exponent to raise this integer to.- Returns:
- The result. Returns 1 if "powerSmall" is 0.
-
PowBigIntVar
public EInteger PowBigIntVar(EInteger power)
Raises an arbitrary-precision integer to a power, which is given as another arbitrary-precision integer.- Parameters:
power- The exponent to raise to.- Returns:
- The result. Returns 1 if "power" is 0.
- Throws:
java.lang.IllegalArgumentException- The parameterpoweris less than 0.java.lang.NullPointerException- The parameterpoweris null.
-
Remainder
public EInteger Remainder(EInteger divisor)
Finds the remainder that results when this instance is divided by the value of an arbitrary-precision integer. The remainder is the value that remains when the absolute value of this object is divided by the absolute value of the other object; the remainder has the same sign (positive or negative) as this object.- Parameters:
divisor- The number to divide by.- Returns:
- The remainder of the two numbers.
- Throws:
java.lang.ArithmeticException- Attempted to divide by zero.java.lang.NullPointerException- The parameterdivisoris null.
-
ShiftRight
public EInteger ShiftRight(EInteger eshift)
Returns an arbitrary-precision integer with the bits shifted to the right. For this operation, the arbitrary-precision integer is treated as a two's-complement form (see"Forms of numbers"). Thus, for negative values, the arbitrary-precision integer is sign-extended.- Parameters:
eshift- The number of bits to shift. Can be negative, in which case this is the same as shiftLeft with the absolute value of this parameter.- Returns:
- An arbitrary-precision integer.
- Throws:
java.lang.NullPointerException- The parametereshiftis null.
-
ShiftLeft
public EInteger ShiftLeft(EInteger eshift)
Returns an arbitrary-precision integer with the bits shifted to the left by a number of bits given as an arbitrary-precision integer. A value of 1 doubles this value, a value of 2 multiplies it by 4, a value of 3 by 8, a value of 4 by 16, and so on.- Parameters:
eshift- The number of bits to shift. Can be negative, in which case this is the same as shiftRight with the absolute value of this parameter.- Returns:
- An arbitrary-precision integer.
- Throws:
java.lang.NullPointerException- The parametereshiftis null.
-
ShiftLeft
public EInteger ShiftLeft(int numberBits)
Returns an arbitrary-precision integer with the bits shifted to the left by a number of bits. A value of 1 doubles this value, a value of 2 multiplies it by 4, a value of 3 by 8, a value of 4 by 16, and so on.- Parameters:
numberBits- The number of bits to shift. Can be negative, in which case this is the same as shiftRight with the absolute value of this parameter.- Returns:
- An arbitrary-precision integer.
-
Not
public EInteger Not()
Returns an arbitrary-precision integer with every bit flipped from this one.- Returns:
- An arbitrary-precision integer.
-
And
public EInteger And(EInteger other)
Does an AND operation between two arbitrary-precision integer values.Each arbitrary-precision integer is treated as a two's-complement form (see
"Forms of numbers") for the purposes of this operator.- Parameters:
other- An arbitrary-precision integer.- Returns:
- An arbitrary-precision integer in which each bit is set if the corresponding bits of this integer and the other integer are both set.
- Throws:
java.lang.NullPointerException- The parameterotheris null.
-
Or
public EInteger Or(EInteger second)
Does an OR operation between two arbitrary-precision integer instances.Each arbitrary-precision integer is treated as a two's-complement form (see
"Forms of numbers") for the purposes of this operator.- Parameters:
second- The second operand.- Returns:
- An arbitrary-precision integer.
- Throws:
java.lang.NullPointerException- The parametersecondis null.java.lang.IllegalArgumentException- Doesn't satisfy biggerCount>0; doesn't satisfy biggerCount == CountWords(result).
-
Xor
public EInteger Xor(EInteger other)
Finds the exclusive "or" of two arbitrary-precision integer objects.Each arbitrary-precision integer is treated as a two's-complement form (see
"Forms of numbers") for the purposes of this operator.- Parameters:
other- An arbitrary-precision integer.- Returns:
- An arbitrary-precision integer in which each bit is set if the corresponding bit is set in one input integer but not in the other.
- Throws:
java.lang.NullPointerException- The parameterotheris null.
-
ShiftRight
public EInteger ShiftRight(int numberBits)
Returns an arbitrary-precision integer with the bits shifted to the right. For this operation, the arbitrary-precision integer is treated as a two's-complement form (see"Forms of numbers"). Thus, for negative values, the arbitrary-precision integer is sign-extended.- Parameters:
numberBits- The number of bits to shift. Can be negative, in which case this is the same as shiftLeft with the absolute value of this parameter.- Returns:
- An arbitrary-precision integer.
-
Sqrt
public EInteger Sqrt()
Finds the square root of this instance's value, rounded down.- Returns:
- The square root of this object's value. Returns 0 if this value is 0 or less.
-
SqrtRem
public EInteger[] SqrtRem()
Calculates the square root and the remainder.- Returns:
- An array of two arbitrary-precision integers: the first integer is the square root, and the second is the difference between this value and the square of the first integer. Returns two zeros if this value is 0 or less, or one and zero if this value equals 1.
-
Subtract
public EInteger Subtract(EInteger subtrahend)
Subtracts an arbitrary-precision integer from this arbitrary-precision integer.- Parameters:
subtrahend- Another arbitrary-precision integer.- Returns:
- The difference of the two objects.
- Throws:
java.lang.NullPointerException- The parametersubtrahendis null.
-
ToBytes
public byte[] ToBytes(boolean littleEndian)
Returns a byte array of this integer's value. The byte array will take the number's two' s-complement form (see"Forms of numbers"), using the fewest bytes necessary to store its value unambiguously. If this value is negative, the bits that appear beyond the most significant bit of the number will be all ones. The resulting byte array can be passed to theFromBytes()method (with the same byte order) to reconstruct this integer's value.- Parameters:
littleEndian- Eithertrueorfalse.- Returns:
- A byte array. If this value is 0, returns a byte array with the single element 0.
-
ToInt32Checked
public int ToInt32Checked()
Converts this object's value to a 32-bit signed integer, throwing an exception if it can't fit.- Returns:
- A 32-bit signed integer.
-
ToInt32Unchecked
public int ToInt32Unchecked()
Converts this object's value to a 32-bit signed integer. If the value can't fit in a 32-bit integer, returns the lower 32 bits of this object's two' s-complement form (see"Forms of numbers") (in which case the return value might have a different sign than this object's value).- Returns:
- A 32-bit signed integer.
-
ToInt64Checked
public long ToInt64Checked()
Converts this object's value to a 64-bit signed integer, throwing an exception if it can't fit.- Returns:
- A 64-bit signed integer.
-
ToInt64Unchecked
public long ToInt64Unchecked()
Converts this object's value to a 64-bit signed integer. If the value can't fit in a 64-bit integer, returns the lower 64 bits of this object's two' s-complement form (see"Forms of numbers") (in which case the return value might have a different sign than this object's value).- Returns:
- A 64-bit signed integer.
-
ToRadixString
public java.lang.String ToRadixString(int radix)
Generates a string representing the value of this object, in the given radix.- Parameters:
radix- A radix from 2 through 36. For example, to generate a hexadecimal (base-16) string, specify 16. To generate a decimal (base-10) string, specify 10.- Returns:
- A string representing the value of this object. If this value is 0, returns "0". If negative, the string will begin with a minus sign ("-", U+002D). Depending on the radix, the string will use the basic digits 0 to 9 (U + 0030 to U + 0039) and then the basic letters A to Z (U + 0041 to U + 005A). For example, 0-9 in radix 10, and 0-9, then A-F in radix 16.
-
toString
public java.lang.String toString()
Converts this object to a text string in base 10.- Overrides:
toStringin classjava.lang.Object- Returns:
- A string representation of this object. If negative, the string will begin with a minus sign ("-", U+002D). The string will use the basic digits 0 to 9 (U + 0030 to U + 0039).
-
ToByteChecked
public byte ToByteChecked()
Converts this number's value to a byte (from 0 to 255) if it can fit in a byte (from 0 to 255).- Returns:
- This number's value as a byte (from 0 to 255).
- Throws:
java.lang.ArithmeticException- This value is less than 0 or greater than 255.
-
ToByteUnchecked
public byte ToByteUnchecked()
Converts this number to a byte (from 0 to 255), returning the least-significant bits of this number's two's-complement form.- Returns:
- This number, converted to a byte (from 0 to 255).
-
FromByte
public static EInteger FromByte(byte inputByte)
Converts a byte (from 0 to 255) to an arbitrary-precision integer.- Parameters:
inputByte- The number to convert as a byte (from 0 to 255).- Returns:
- This number's value as an arbitrary-precision integer.
-
ToInt16Checked
public short ToInt16Checked()
Converts this number's value to a 16-bit signed integer if it can fit in a 16-bit signed integer.- Returns:
- This number's value as a 16-bit signed integer.
- Throws:
java.lang.ArithmeticException- This value is less than -32768 or greater than 32767.
-
ToInt16Unchecked
public short ToInt16Unchecked()
Converts this number to a 16-bit signed integer, returning the least-significant bits of this number's two's-complement form.- Returns:
- This number, converted to a 16-bit signed integer.
-
FromInt16
public static EInteger FromInt16(short inputInt16)
Converts a 16-bit signed integer to an arbitrary-precision integer.- Parameters:
inputInt16- The number to convert as a 16-bit signed integer.- Returns:
- This number's value as an arbitrary-precision integer.
-
-