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:
EInteger
objects 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 EInteger
Abs()
Returns the absolute value of this object's value.EInteger
Add(int intValue)
Adds this object and another object.EInteger
Add(EInteger bigintAugend)
Adds this object and another object.EInteger
And(EInteger other)
Does an AND operation between two arbitrary-precision integer values.int
AsInt32Checked()
Deprecated.Renamed to ToInt32Checked.int
AsInt32Unchecked()
Deprecated.Renamed to ToInt32Unchecked.long
AsInt64Checked()
Deprecated.Renamed to ToInt64Checked.long
AsInt64Unchecked()
Deprecated.Renamed to ToInt64Unchecked.boolean
CanFitInInt32()
Returns whether this object's value can fit in a 32-bit signed integer.boolean
CanFitInInt64()
Returns whether this object's value can fit in a 64-bit signed integer.int
compareTo(int intValue)
Compares an arbitrary-precision integer with this instance.int
compareTo(EInteger other)
Compares an arbitrary-precision integer with this instance.EInteger
Divide(int intValue)
Divides this instance by the value of an arbitrary-precision integer.EInteger
Divide(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.boolean
equals(java.lang.Object obj)
Determines whether this object and another object are equal and have the same type.static EInteger
FromBoolean(boolean boolValue)
Converts a boolean value (true or false) to an arbitrary-precision integer.static EInteger
FromByte(byte inputByte)
Converts a byte (from 0 to 255) to an arbitrary-precision integer.static EInteger
FromBytes(byte[] bytes, boolean littleEndian)
Initializes an arbitrary-precision integer from an array of bytes.static EInteger
FromInt16(short inputInt16)
Converts a 16-bit signed integer to an arbitrary-precision integer.static EInteger
FromInt32(int intValue)
Converts a 32-bit signed integer to an arbitrary-precision integer.static EInteger
FromInt64(long longerValue)
Converts a 64-bit signed integer to an arbitrary-precision integer.static EInteger
FromRadixString(java.lang.String str, int radix)
Converts a string to an arbitrary-precision integer in a given radix.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.static EInteger
FromString(java.lang.String str)
Converts a string to an arbitrary-precision integer.static EInteger
FromSubstring(java.lang.String str, int index, int endIndex)
Converts a portion of a string to an arbitrary-precision integer.EInteger
Gcd(EInteger bigintSecond)
Returns the greatest common divisor of this integer and the given integer.int
GetDigitCount()
Deprecated.This method may overflow.EInteger
GetDigitCountAsEInteger()
Returns the number of decimal digits used by this integer, in the form of an arbitrary-precision integer.int
GetLowBit()
Deprecated.This method may overflow.EInteger
GetLowBitAsEInteger()
Gets the lowest set bit in this number's absolute value, in the form of an arbitrary-precision integer.static EInteger
getOne()
Gets the number 1 as an arbitrary-precision integer.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.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.int
GetSignedBitLength()
Deprecated.This method may overflow.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.static EInteger
getTen()
Gets the number 10 as an arbitrary-precision integer.boolean
GetUnsignedBit(int index)
Returns whether a bit is set in this number's absolute value.boolean
GetUnsignedBit(EInteger bigIndex)
Returns whether a bit is set in this number's absolute value.int
GetUnsignedBitLength()
Deprecated.This method may overflow.EInteger
GetUnsignedBitLengthAsEInteger()
Finds the minimum number of bits needed to represent this number's absolute value.static EInteger
getZero()
Gets the number zero as an arbitrary-precision integer.int
hashCode()
Returns the hash code for this instance.boolean
isEven()
Gets a value indicating whether this value is even.boolean
isPowerOfTwo()
Gets a value indicating whether this object's value is a power of two, and greater than 0.boolean
isZero()
Gets a value indicating whether this value is 0.EInteger
Mod(int smallDivisor)
Finds the modulus remainder that results when this instance is divided by the value of another integer.EInteger
Mod(EInteger divisor)
Finds the modulus remainder that results when this instance is divided by the value of an arbitrary-precision integer.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.EInteger
Multiply(int intValue)
Multiplies this instance by the value of an arbitrary-precision integer object.EInteger
Multiply(EInteger bigintMult)
Multiplies this instance by the value of an arbitrary-precision integer object.EInteger
Negate()
Gets the value of this object with the sign reversed.EInteger
Not()
Returns an arbitrary-precision integer with every bit flipped from this one.EInteger
Or(EInteger second)
Does an OR operation between two arbitrary-precision integer instances.EInteger
Pow(int powerSmall)
Raises an arbitrary-precision integer to a power.EInteger
Pow(EInteger bigPower)
Raises an arbitrary-precision integer to a power.EInteger
PowBigIntVar(EInteger power)
Raises an arbitrary-precision integer to a power, which is given as another arbitrary-precision integer.EInteger
Remainder(int intValue)
Finds the remainder that results when this instance is divided by the value of an arbitrary-precision integer.EInteger
Remainder(EInteger divisor)
Finds the remainder that results when this instance is divided by the value of an arbitrary-precision integer.EInteger
ShiftLeft(int numberBits)
Returns an arbitrary-precision integer with the bits shifted to the left by a number of bits.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.EInteger
ShiftRight(int numberBits)
Returns an arbitrary-precision integer with the bits shifted to the right.EInteger
ShiftRight(EInteger eshift)
Returns an arbitrary-precision integer with the bits shifted to the right.int
signum()
Gets the sign of this object's value.EInteger
Sqrt()
Finds the square root of this instance's value, rounded down.EInteger[]
SqrtRem()
Calculates the square root and the remainder.EInteger
Subtract(int intValue)
Subtracts an arbitrary-precision integer from this arbitrary-precision integer.EInteger
Subtract(EInteger subtrahend)
Subtracts an arbitrary-precision integer from this arbitrary-precision integer.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).byte[]
ToBytes(boolean littleEndian)
Returns a byte array of this integer's value.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.short
ToInt16Checked()
Converts this number's value to a 16-bit signed integer if it can fit in a 16-bit signed integer.short
ToInt16Unchecked()
Converts this number to a 16-bit signed integer, returning the least-significant bits of this number's two's-complement form.int
ToInt32Checked()
Converts this object's value to a 32-bit signed integer, throwing an exception if it can't fit.int
ToInt32Unchecked()
Converts this object's value to a 32-bit signed integer.long
ToInt64Checked()
Converts this object's value to a 64-bit signed integer, throwing an exception if it can't fit.long
ToInt64Unchecked()
Converts this object's value to a 64-bit signed integer.java.lang.String
ToRadixString(int radix)
Generates a string representing the value of this object, in the given radix.java.lang.String
toString()
Converts this object to a text string in base 10.EInteger
Xor(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:
true
if 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:
true
if 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:
true
if 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, 0x2c
and 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, 0x70
and -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 parameterbytes
is 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
boolValue
is true; otherwise, 0.
-
FromInt32
public static EInteger FromInt32(int intValue)
Converts a 32-bit signed integer to an arbitrary-precision integer.- Parameters:
intValue
- The parameterintValue
is 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 parameterlongerValue
is 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 parameterstr
is 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 parameterstr
is 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 parameterstr
is in an invalid format.java.lang.NullPointerException
- The parameterstr
is 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 parameterindex
is less than 0,endIndex
is less than 0, or either is greater than the string's length, orendIndex
is less thanindex
.java.lang.NullPointerException
- The parameterstr
is 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 parameterbigintAugend
is 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:
true
if 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:
true
if 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:
compareTo
in 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 parameterintValue
is 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 parameterintValue
is 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 parameterintValue
is 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 parameterintValue
is 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 parameterintValue
is null.
-
compareTo
public int compareTo(int intValue)
Compares an arbitrary-precision integer with this instance.- Parameters:
intValue
- The parameterintValue
is 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 parameterbigintDivisor
is 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 parameterdivisor
is null.
-
equals
public boolean equals(java.lang.Object obj)
Determines whether this object and another object are equal and have the same type.- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- The parameterobj
is an arbitrary object.- Returns:
true
if 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 parameterbigintSecond
is 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:
hashCode
in 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:
true
if a bit is set in the two' s-complement form (seeEDecimal
) of this object' s value; otherwise,false
.- Throws:
java.lang.NullPointerException
- The parameterbigIndex
is 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 parameterindex
is a 32-bit signed integer.- Returns:
true
if 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:
true
if a bit is set in this number's absolute value.- Throws:
java.lang.NullPointerException
- The parameterbigIndex
is 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:
true
if 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 parameterdivisor
is less than 0.java.lang.NullPointerException
- The parameterdivisor
is 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 parametersmallDivisor
is 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 parameterpow
ormod
is 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 parameterbigintMult
is 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
bigPower
is 0. - Throws:
java.lang.NullPointerException
- The parameterbigPower
is 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 parameterpower
is less than 0.java.lang.NullPointerException
- The parameterpower
is 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 parameterdivisor
is 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 parametereshift
is 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 parametereshift
is 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 parameterother
is 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 parametersecond
is 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 parameterother
is 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 parametersubtrahend
is 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
- Eithertrue
orfalse
.- 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:
toString
in 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.
-
-