An arbitrarily large integer class. More...
Public Member Functions | |
| BigInteger () | |
| Creates an empty BigInteger. | |
| BigInteger (unsigned int value) | |
| Creates a BigInteger containing an integer value in its low bits. | |
| BigInteger (int value) | |
| Creates a BigInteger containing an integer value in its low bits. | |
| BigInteger (int64 value) | |
| Creates a BigInteger containing an integer value in its low bits. | |
| BigInteger (const BigInteger &other) | |
| Creates a copy of another BigInteger. | |
| ~BigInteger () | |
| Destructor. | |
| BigInteger & | operator= (const BigInteger &other) |
| Copies another BigInteger onto this one. | |
| void | swapWith (BigInteger &other) throw () |
| Swaps the internal contents of this with another object. | |
| bool | operator[] (int bit) const throw () |
| Returns the value of a specified bit in the number. | |
| bool | isZero () const throw () |
| Returns true if no bits are set. | |
| bool | isOne () const throw () |
| Returns true if the value is 1. | |
| int | toInteger () const throw () |
| Attempts to get the lowest bits of the value as an integer. | |
| void | clear () |
| Resets the value to 0. | |
| void | clearBit (int bitNumber) throw () |
| Clears a particular bit in the number. | |
| void | setBit (int bitNumber) |
| Sets a specified bit to 1. | |
| void | setBit (int bitNumber, bool shouldBeSet) |
| Sets or clears a specified bit. | |
| void | setRange (int startBit, int numBits, bool shouldBeSet) |
| Sets a range of bits to be either on or off. | |
| void | insertBit (int bitNumber, bool shouldBeSet) |
| Inserts a bit an a given position, shifting up any bits above it. | |
| const BigInteger | getBitRange (int startBit, int numBits) const |
| Returns a range of bits as a new BigInteger. | |
| int | getBitRangeAsInt (int startBit, int numBits) const throw () |
| Returns a range of bits as an integer value. | |
| void | setBitRangeAsInt (int startBit, int numBits, unsigned int valueToSet) |
| Sets a range of bits to an integer value. | |
| void | shiftBits (int howManyBitsLeft, int startBit) |
| Shifts a section of bits left or right. | |
| int | countNumberOfSetBits () const throw () |
| Returns the total number of set bits in the value. | |
| int | findNextSetBit (int startIndex=0) const throw () |
| Looks for the index of the next set bit after a given starting point. | |
| int | findNextClearBit (int startIndex=0) const throw () |
| Looks for the index of the next clear bit after a given starting point. | |
| int | getHighestBit () const throw () |
| Returns the index of the highest set bit in the number. | |
| BigInteger & | operator+= (const BigInteger &other) |
| BigInteger & | operator-= (const BigInteger &other) |
| BigInteger & | operator*= (const BigInteger &other) |
| BigInteger & | operator/= (const BigInteger &other) |
| BigInteger & | operator|= (const BigInteger &other) |
| BigInteger & | operator&= (const BigInteger &other) |
| BigInteger & | operator^= (const BigInteger &other) |
| BigInteger & | operator%= (const BigInteger &other) |
| BigInteger & | operator<<= (int numBitsToShift) |
| BigInteger & | operator>>= (int numBitsToShift) |
| BigInteger & | operator++ () |
| BigInteger & | operator-- () |
| const BigInteger | operator++ (int) |
| const BigInteger | operator-- (int) |
| const BigInteger | operator- () const |
| const BigInteger | operator+ (const BigInteger &other) const |
| const BigInteger | operator- (const BigInteger &other) const |
| const BigInteger | operator* (const BigInteger &other) const |
| const BigInteger | operator/ (const BigInteger &other) const |
| const BigInteger | operator| (const BigInteger &other) const |
| const BigInteger | operator& (const BigInteger &other) const |
| const BigInteger | operator^ (const BigInteger &other) const |
| const BigInteger | operator% (const BigInteger &other) const |
| const BigInteger | operator<< (int numBitsToShift) const |
| const BigInteger | operator>> (int numBitsToShift) const |
| bool | operator== (const BigInteger &other) const throw () |
| bool | operator!= (const BigInteger &other) const throw () |
| bool | operator< (const BigInteger &other) const throw () |
| bool | operator<= (const BigInteger &other) const throw () |
| bool | operator> (const BigInteger &other) const throw () |
| bool | operator>= (const BigInteger &other) const throw () |
| int | compare (const BigInteger &other) const throw () |
| Does a signed comparison of two BigIntegers. | |
| int | compareAbsolute (const BigInteger &other) const throw () |
| Compares the magnitudes of two BigIntegers, ignoring their signs. | |
| void | divideBy (const BigInteger &divisor, BigInteger &remainder) |
| Divides this value by another one and returns the remainder. | |
| const BigInteger | findGreatestCommonDivisor (BigInteger other) const |
| Returns the largest value that will divide both this value and the one passed-in. | |
| void | exponentModulo (const BigInteger &exponent, const BigInteger &modulus) |
| Performs a combined exponent and modulo operation. | |
| void | inverseModulo (const BigInteger &modulus) |
| Performs an inverse modulo on the value. | |
| bool | isNegative () const throw () |
| Returns true if the value is less than zero. | |
| void | setNegative (const bool shouldBeNegative) throw () |
| Changes the sign of the number to be positive or negative. | |
| void | negate () throw () |
| Inverts the sign of the number. | |
| const String | toString (int base, int minimumNumCharacters=1) const |
| Converts the number to a string. | |
| void | parseString (const String &text, int base) |
| Reads the numeric value from a string. | |
| const MemoryBlock | toMemoryBlock () const |
| Turns the number into a block of binary data. | |
| void | loadFromMemoryBlock (const MemoryBlock &data) |
| Converts a block of raw data into a number. | |
An arbitrarily large integer class.
A BigInteger can be used in a similar way to a normal integer, but has no size limit (except for memory and performance constraints).
Negative values are possible, but the value isn't stored as 2s-complement, so be careful if you use negative values and look at the values of individual bits.
| BigInteger::BigInteger | ( | ) |
Creates an empty BigInteger.
| BigInteger::BigInteger | ( | unsigned int | value | ) |
Creates a BigInteger containing an integer value in its low bits.
The low 32 bits of the number are initialised with this value.
| BigInteger::BigInteger | ( | int | value | ) |
Creates a BigInteger containing an integer value in its low bits.
The low 32 bits of the number are initialised with the absolute value passed in, and its sign is set to reflect the sign of the number.
| BigInteger::BigInteger | ( | int64 | value | ) |
Creates a BigInteger containing an integer value in its low bits.
The low 64 bits of the number are initialised with the absolute value passed in, and its sign is set to reflect the sign of the number.
| BigInteger::BigInteger | ( | const BigInteger & | other | ) |
Creates a copy of another BigInteger.
| BigInteger::~BigInteger | ( | ) |
Destructor.
| BigInteger& BigInteger::operator= | ( | const BigInteger & | other | ) |
Copies another BigInteger onto this one.
| void BigInteger::swapWith | ( | BigInteger & | other | ) | throw () |
Swaps the internal contents of this with another object.
| bool BigInteger::operator[] | ( | int | bit | ) | const throw () |
Returns the value of a specified bit in the number.
If the index is out-of-range, the result will be false.
| bool BigInteger::isZero | ( | ) | const throw () |
Returns true if no bits are set.
| bool BigInteger::isOne | ( | ) | const throw () |
Returns true if the value is 1.
| int BigInteger::toInteger | ( | ) | const throw () |
Attempts to get the lowest bits of the value as an integer.
If the value is bigger than the integer limits, this will return only the lower bits.
| void BigInteger::clear | ( | ) |
Resets the value to 0.
| void BigInteger::clearBit | ( | int | bitNumber | ) | throw () |
Clears a particular bit in the number.
| void BigInteger::setBit | ( | int | bitNumber | ) |
Sets a specified bit to 1.
| void BigInteger::setBit | ( | int | bitNumber, | |
| bool | shouldBeSet | |||
| ) |
Sets or clears a specified bit.
| void BigInteger::setRange | ( | int | startBit, | |
| int | numBits, | |||
| bool | shouldBeSet | |||
| ) |
Sets a range of bits to be either on or off.
| startBit | the first bit to change | |
| numBits | the number of bits to change | |
| shouldBeSet | whether to turn these bits on or off |
| void BigInteger::insertBit | ( | int | bitNumber, | |
| bool | shouldBeSet | |||
| ) |
Inserts a bit an a given position, shifting up any bits above it.
| const BigInteger BigInteger::getBitRange | ( | int | startBit, | |
| int | numBits | |||
| ) | const |
Returns a range of bits as a new BigInteger.
e.g. getBitRangeAsInt (0, 64) would return the lowest 64 bits.
| int BigInteger::getBitRangeAsInt | ( | int | startBit, | |
| int | numBits | |||
| ) | const throw () |
Returns a range of bits as an integer value.
e.g. getBitRangeAsInt (0, 32) would return the lowest 32 bits.
Asking for more than 32 bits isn't allowed (obviously) - for that, use getBitRange().
| void BigInteger::setBitRangeAsInt | ( | int | startBit, | |
| int | numBits, | |||
| unsigned int | valueToSet | |||
| ) |
Sets a range of bits to an integer value.
Copies the given integer onto a range of bits, starting at startBit, and using up to numBits of the available bits.
| void BigInteger::shiftBits | ( | int | howManyBitsLeft, | |
| int | startBit | |||
| ) |
Shifts a section of bits left or right.
| howManyBitsLeft | how far to move the bits (+ve numbers shift it left, -ve numbers shift it right). | |
| startBit | the first bit to affect - if this is > 0, only bits above that index will be affected. |
| int BigInteger::countNumberOfSetBits | ( | ) | const throw () |
Returns the total number of set bits in the value.
| int BigInteger::findNextSetBit | ( | int | startIndex = 0 |
) | const throw () |
Looks for the index of the next set bit after a given starting point.
This searches from startIndex (inclusive) upwards for the first set bit, and returns its index. If no set bits are found, it returns -1.
| int BigInteger::findNextClearBit | ( | int | startIndex = 0 |
) | const throw () |
Looks for the index of the next clear bit after a given starting point.
This searches from startIndex (inclusive) upwards for the first clear bit, and returns its index.
| int BigInteger::getHighestBit | ( | ) | const throw () |
Returns the index of the highest set bit in the number.
If the value is zero, this will return -1.
| BigInteger& BigInteger::operator+= | ( | const BigInteger & | other | ) |
| BigInteger& BigInteger::operator-= | ( | const BigInteger & | other | ) |
| BigInteger& BigInteger::operator*= | ( | const BigInteger & | other | ) |
| BigInteger& BigInteger::operator/= | ( | const BigInteger & | other | ) |
| BigInteger& BigInteger::operator|= | ( | const BigInteger & | other | ) |
| BigInteger& BigInteger::operator&= | ( | const BigInteger & | other | ) |
| BigInteger& BigInteger::operator^= | ( | const BigInteger & | other | ) |
| BigInteger& BigInteger::operator%= | ( | const BigInteger & | other | ) |
| BigInteger& BigInteger::operator<<= | ( | int | numBitsToShift | ) |
| BigInteger& BigInteger::operator>>= | ( | int | numBitsToShift | ) |
| BigInteger& BigInteger::operator++ | ( | ) |
| BigInteger& BigInteger::operator-- | ( | ) |
| const BigInteger BigInteger::operator++ | ( | int | ) |
| const BigInteger BigInteger::operator-- | ( | int | ) |
| const BigInteger BigInteger::operator- | ( | ) | const |
| const BigInteger BigInteger::operator+ | ( | const BigInteger & | other | ) | const |
| const BigInteger BigInteger::operator- | ( | const BigInteger & | other | ) | const |
| const BigInteger BigInteger::operator* | ( | const BigInteger & | other | ) | const |
| const BigInteger BigInteger::operator/ | ( | const BigInteger & | other | ) | const |
| const BigInteger BigInteger::operator| | ( | const BigInteger & | other | ) | const |
| const BigInteger BigInteger::operator& | ( | const BigInteger & | other | ) | const |
| const BigInteger BigInteger::operator^ | ( | const BigInteger & | other | ) | const |
| const BigInteger BigInteger::operator% | ( | const BigInteger & | other | ) | const |
| const BigInteger BigInteger::operator<< | ( | int | numBitsToShift | ) | const |
| const BigInteger BigInteger::operator>> | ( | int | numBitsToShift | ) | const |
| bool BigInteger::operator== | ( | const BigInteger & | other | ) | const throw () |
| bool BigInteger::operator!= | ( | const BigInteger & | other | ) | const throw () |
| bool BigInteger::operator< | ( | const BigInteger & | other | ) | const throw () |
| bool BigInteger::operator<= | ( | const BigInteger & | other | ) | const throw () |
| bool BigInteger::operator> | ( | const BigInteger & | other | ) | const throw () |
| bool BigInteger::operator>= | ( | const BigInteger & | other | ) | const throw () |
| int BigInteger::compare | ( | const BigInteger & | other | ) | const throw () |
Does a signed comparison of two BigIntegers.
Return values are:
| int BigInteger::compareAbsolute | ( | const BigInteger & | other | ) | const throw () |
Compares the magnitudes of two BigIntegers, ignoring their signs.
Return values are:
| void BigInteger::divideBy | ( | const BigInteger & | divisor, | |
| BigInteger & | remainder | |||
| ) |
Divides this value by another one and returns the remainder.
This number is divided by other, leaving the quotient in this number, with the remainder being copied to the other BigInteger passed in.
| const BigInteger BigInteger::findGreatestCommonDivisor | ( | BigInteger | other | ) | const |
Returns the largest value that will divide both this value and the one passed-in.
| void BigInteger::exponentModulo | ( | const BigInteger & | exponent, | |
| const BigInteger & | modulus | |||
| ) |
Performs a combined exponent and modulo operation.
This BigInteger's value becomes (this ^ exponent) % modulus.
| void BigInteger::inverseModulo | ( | const BigInteger & | modulus | ) |
Performs an inverse modulo on the value.
i.e. the result is (this ^ -1) mod (modulus).
| bool BigInteger::isNegative | ( | ) | const throw () |
Returns true if the value is less than zero.
| void BigInteger::setNegative | ( | const bool | shouldBeNegative | ) | throw () |
Changes the sign of the number to be positive or negative.
| void BigInteger::negate | ( | ) | throw () |
Inverts the sign of the number.
| const String BigInteger::toString | ( | int | base, | |
| int | minimumNumCharacters = 1 | |||
| ) | const |
Converts the number to a string.
Specify a base such as 2 (binary), 8 (octal), 10 (decimal), 16 (hex). If minimumNumCharacters is greater than 0, the returned string will be padded with leading zeros to reach at least that length.
| void BigInteger::parseString | ( | const String & | text, | |
| int | base | |||
| ) |
Reads the numeric value from a string.
Specify a base such as 2 (binary), 8 (octal), 10 (decimal), 16 (hex). Any invalid characters will be ignored.
| const MemoryBlock BigInteger::toMemoryBlock | ( | ) | const |
Turns the number into a block of binary data.
The data is arranged as little-endian, so the first byte of data is the low 8 bits of the number, and so on.
| void BigInteger::loadFromMemoryBlock | ( | const MemoryBlock & | data | ) |
Converts a block of raw data into a number.
The data is arranged as little-endian, so the first byte of data is the low 8 bits of the number, and so on.
1.6.3