Loading...
Searching...
No Matches
BigInteger Class Reference

An arbitrarily large integer class. More...

Public Member Functions

 BigInteger ()
 Creates an empty BigInteger.
 
 BigInteger (uint32 value)
 Creates a BigInteger containing an integer value in its low bits.
 
 BigInteger (int32 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 &)
 Creates a copy of another BigInteger.
 
 BigInteger (BigInteger &&) noexcept
 Move constructor.
 
BigIntegeroperator= (BigInteger &&) noexcept
 Move assignment operator.
 
 ~BigInteger ()
 Destructor.
 
BigIntegeroperator= (const BigInteger &)
 Copies another BigInteger onto this one.
 
void swapWith (BigInteger &) noexcept
 Swaps the internal contents of this with another object.
 
bool operator[] (int bit) const noexcept
 Returns the value of a specified bit in the number.
 
bool isZero () const noexcept
 Returns true if no bits are set.
 
bool isOne () const noexcept
 Returns true if the value is 1.
 
int toInteger () const noexcept
 Attempts to get the lowest 32 bits of the value as an integer.
 
int64 toInt64 () const noexcept
 Attempts to get the lowest 64 bits of the value as an integer.
 
BigIntegerclear () noexcept
 Resets the value to 0.
 
BigIntegerclearBit (int bitNumber) noexcept
 Clears a particular bit in the number.
 
BigIntegersetBit (int bitNumber)
 Sets a specified bit to 1.
 
BigIntegersetBit (int bitNumber, bool shouldBeSet)
 Sets or clears a specified bit.
 
BigIntegersetRange (int startBit, int numBits, bool shouldBeSet)
 Sets a range of bits to be either on or off.
 
BigIntegerinsertBit (int bitNumber, bool shouldBeSet)
 Inserts a bit an a given position, shifting up any bits above it.
 
BigInteger getBitRange (int startBit, int numBits) const
 Returns a range of bits as a new BigInteger.
 
uint32 getBitRangeAsInt (int startBit, int numBits) const noexcept
 Returns a range of bits as an integer value.
 
BigIntegersetBitRangeAsInt (int startBit, int numBits, uint32 valueToSet)
 Sets a range of bits to an integer value.
 
BigIntegershiftBits (int howManyBitsLeft, int startBit)
 Shifts a section of bits left or right.
 
int countNumberOfSetBits () const noexcept
 Returns the total number of set bits in the value.
 
int findNextSetBit (int startIndex) const noexcept
 Looks for the index of the next set bit after a given starting point.
 
int findNextClearBit (int startIndex) const noexcept
 Looks for the index of the next clear bit after a given starting point.
 
int getHighestBit () const noexcept
 Returns the index of the highest set bit in the number.
 
bool isNegative () const noexcept
 Returns true if the value is less than zero.
 
void setNegative (bool shouldBeNegative) noexcept
 Changes the sign of the number to be positive or negative.
 
void negate () noexcept
 Inverts the sign of the number.
 
BigIntegeroperator+= (const BigInteger &)
 
BigIntegeroperator-= (const BigInteger &)
 
BigIntegeroperator*= (const BigInteger &)
 
BigIntegeroperator/= (const BigInteger &)
 
BigIntegeroperator|= (const BigInteger &)
 
BigIntegeroperator&= (const BigInteger &)
 
BigIntegeroperator^= (const BigInteger &)
 
BigIntegeroperator%= (const BigInteger &)
 
BigIntegeroperator<<= (int numBitsToShift)
 
BigIntegeroperator>>= (int numBitsToShift)
 
BigIntegeroperator++ ()
 
BigIntegeroperator-- ()
 
BigInteger operator++ (int)
 
BigInteger operator-- (int)
 
BigInteger operator- () const
 
BigInteger operator+ (const BigInteger &) const
 
BigInteger operator- (const BigInteger &) const
 
BigInteger operator* (const BigInteger &) const
 
BigInteger operator/ (const BigInteger &) const
 
BigInteger operator| (const BigInteger &) const
 
BigInteger operator& (const BigInteger &) const
 
BigInteger operator^ (const BigInteger &) const
 
BigInteger operator% (const BigInteger &) const
 
BigInteger operator<< (int numBitsToShift) const
 
BigInteger operator>> (int numBitsToShift) const
 
bool operator== (const BigInteger &) const noexcept
 
bool operator!= (const BigInteger &) const noexcept
 
bool operator< (const BigInteger &) const noexcept
 
bool operator<= (const BigInteger &) const noexcept
 
bool operator> (const BigInteger &) const noexcept
 
bool operator>= (const BigInteger &) const noexcept
 
int compare (const BigInteger &other) const noexcept
 Does a signed comparison of two BigIntegers.
 
int compareAbsolute (const BigInteger &other) const noexcept
 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.
 
BigInteger findGreatestCommonDivisor (BigInteger other) const
 Returns the largest value that will divide both this value and the argument.
 
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.
 
void montgomeryMultiplication (const BigInteger &other, const BigInteger &modulus, const BigInteger &modulusp, int k)
 Performs the Montgomery Multiplication with modulo.
 
void extendedEuclidean (const BigInteger &a, const BigInteger &b, BigInteger &xOut, BigInteger &yOut)
 Performs the Extended Euclidean algorithm.
 
String toString (int base, int minimumNumCharacters=1) const
 Converts the number to a string.
 
void parseString (StringRef text, int base)
 Reads the numeric value from a string.
 
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.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ BigInteger() [1/6]

◆ BigInteger() [2/6]

BigInteger::BigInteger ( uint32 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() [3/6]

BigInteger::BigInteger ( int32 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() [4/6]

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() [5/6]

BigInteger::BigInteger ( const BigInteger & )

Creates a copy of another BigInteger.

References BigInteger().

◆ BigInteger() [6/6]

BigInteger::BigInteger ( BigInteger && )
noexcept

Move constructor.

References BigInteger().

◆ ~BigInteger()

BigInteger::~BigInteger ( )

Destructor.

References BigInteger().

Member Function Documentation

◆ operator=() [1/2]

BigInteger & BigInteger::operator= ( BigInteger && )
noexcept

Move assignment operator.

References BigInteger().

◆ operator=() [2/2]

BigInteger & BigInteger::operator= ( const BigInteger & )

Copies another BigInteger onto this one.

References BigInteger().

◆ swapWith()

void BigInteger::swapWith ( BigInteger & )
noexcept

Swaps the internal contents of this with another object.

References BigInteger(), and swapWith().

Referenced by swapWith().

◆ operator[]()

bool BigInteger::operator[] ( int bit) const
noexcept

Returns the value of a specified bit in the number.

If the index is out-of-range, the result will be false.

◆ isZero()

bool BigInteger::isZero ( ) const
noexcept

Returns true if no bits are set.

References isZero().

Referenced by isZero().

◆ isOne()

bool BigInteger::isOne ( ) const
noexcept

Returns true if the value is 1.

References isOne().

Referenced by isOne().

◆ toInteger()

int BigInteger::toInteger ( ) const
noexcept

Attempts to get the lowest 32 bits of the value as an integer.

If the value is bigger than the integer limits, this will return only the lower bits.

References toInteger().

Referenced by toInteger().

◆ toInt64()

int64 BigInteger::toInt64 ( ) const
noexcept

Attempts to get the lowest 64 bits of the value as an integer.

If the value is bigger than the integer limits, this will return only the lower bits.

References toInt64().

Referenced by toInt64().

◆ clear()

BigInteger & BigInteger::clear ( )
noexcept

Resets the value to 0.

References BigInteger(), and clear().

Referenced by clear().

◆ clearBit()

BigInteger & BigInteger::clearBit ( int bitNumber)
noexcept

Clears a particular bit in the number.

References BigInteger(), and clearBit().

Referenced by clearBit().

◆ setBit() [1/2]

BigInteger & BigInteger::setBit ( int bitNumber)

Sets a specified bit to 1.

References BigInteger(), and setBit().

Referenced by setBit(), and setBit().

◆ setBit() [2/2]

BigInteger & BigInteger::setBit ( int bitNumber,
bool shouldBeSet )

Sets or clears a specified bit.

References BigInteger(), and setBit().

◆ setRange()

BigInteger & BigInteger::setRange ( int startBit,
int numBits,
bool shouldBeSet )

Sets a range of bits to be either on or off.

Parameters
startBitthe first bit to change
numBitsthe number of bits to change
shouldBeSetwhether to turn these bits on or off

References BigInteger(), and setRange().

Referenced by setRange().

◆ insertBit()

BigInteger & BigInteger::insertBit ( int bitNumber,
bool shouldBeSet )

Inserts a bit an a given position, shifting up any bits above it.

References BigInteger(), and insertBit().

Referenced by insertBit().

◆ getBitRange()

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.

See also
getBitRangeAsInt

References BigInteger(), and getBitRange().

Referenced by getBitRange().

◆ getBitRangeAsInt()

uint32 BigInteger::getBitRangeAsInt ( int startBit,
int numBits ) const
noexcept

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().

References getBitRangeAsInt().

Referenced by getBitRangeAsInt().

◆ setBitRangeAsInt()

BigInteger & BigInteger::setBitRangeAsInt ( int startBit,
int numBits,
uint32 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.

References BigInteger(), and setBitRangeAsInt().

Referenced by setBitRangeAsInt().

◆ shiftBits()

BigInteger & BigInteger::shiftBits ( int howManyBitsLeft,
int startBit )

Shifts a section of bits left or right.

Parameters
howManyBitsLefthow far to move the bits (+ve numbers shift it left, -ve numbers shift it right).
startBitthe first bit to affect - if this is > 0, only bits above that index will be affected.

References BigInteger(), and shiftBits().

Referenced by shiftBits().

◆ countNumberOfSetBits()

int BigInteger::countNumberOfSetBits ( ) const
noexcept

Returns the total number of set bits in the value.

References countNumberOfSetBits().

Referenced by countNumberOfSetBits().

◆ findNextSetBit()

int BigInteger::findNextSetBit ( int startIndex) const
noexcept

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.

References findNextSetBit().

Referenced by findNextSetBit().

◆ findNextClearBit()

int BigInteger::findNextClearBit ( int startIndex) const
noexcept

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.

References findNextClearBit().

Referenced by findNextClearBit().

◆ getHighestBit()

int BigInteger::getHighestBit ( ) const
noexcept

Returns the index of the highest set bit in the number.

If the value is zero, this will return -1.

References getHighestBit().

Referenced by getHighestBit().

◆ isNegative()

bool BigInteger::isNegative ( ) const
noexcept

Returns true if the value is less than zero.

See also
setNegative, negate

References isNegative().

Referenced by isNegative().

◆ setNegative()

void BigInteger::setNegative ( bool shouldBeNegative)
noexcept

Changes the sign of the number to be positive or negative.

See also
isNegative, negate

References setNegative().

Referenced by setNegative().

◆ negate()

void BigInteger::negate ( )
noexcept

Inverts the sign of the number.

See also
isNegative, setNegative

References negate().

Referenced by negate().

◆ operator+=()

BigInteger & BigInteger::operator+= ( const BigInteger & )

References BigInteger().

◆ operator-=()

BigInteger & BigInteger::operator-= ( const BigInteger & )

References BigInteger().

◆ operator*=()

BigInteger & BigInteger::operator*= ( const BigInteger & )

References BigInteger().

◆ operator/=()

BigInteger & BigInteger::operator/= ( const BigInteger & )

References BigInteger().

◆ operator|=()

BigInteger & BigInteger::operator|= ( const BigInteger & )

References BigInteger().

◆ operator&=()

BigInteger & BigInteger::operator&= ( const BigInteger & )

References BigInteger().

◆ operator^=()

BigInteger & BigInteger::operator^= ( const BigInteger & )

References BigInteger().

◆ operator%=()

BigInteger & BigInteger::operator%= ( const BigInteger & )

References BigInteger().

◆ operator<<=()

BigInteger & BigInteger::operator<<= ( int numBitsToShift)

References BigInteger().

◆ operator>>=()

BigInteger & BigInteger::operator>>= ( int numBitsToShift)

References BigInteger().

◆ operator++() [1/2]

BigInteger & BigInteger::operator++ ( )

References BigInteger().

◆ operator--() [1/2]

BigInteger & BigInteger::operator-- ( )

References BigInteger().

◆ operator++() [2/2]

BigInteger BigInteger::operator++ ( int )

References BigInteger().

◆ operator--() [2/2]

BigInteger BigInteger::operator-- ( int )

References BigInteger().

◆ operator-() [1/2]

BigInteger BigInteger::operator- ( ) const

References BigInteger().

◆ operator+()

BigInteger BigInteger::operator+ ( const BigInteger & ) const

References BigInteger().

◆ operator-() [2/2]

BigInteger BigInteger::operator- ( const BigInteger & ) const

References BigInteger().

◆ operator*()

BigInteger BigInteger::operator* ( const BigInteger & ) const

References BigInteger().

◆ operator/()

BigInteger BigInteger::operator/ ( const BigInteger & ) const

References BigInteger().

◆ operator|()

BigInteger BigInteger::operator| ( const BigInteger & ) const

References BigInteger().

◆ operator&()

BigInteger BigInteger::operator& ( const BigInteger & ) const

References BigInteger().

◆ operator^()

BigInteger BigInteger::operator^ ( const BigInteger & ) const

References BigInteger().

◆ operator%()

BigInteger BigInteger::operator% ( const BigInteger & ) const

References BigInteger().

◆ operator<<()

BigInteger BigInteger::operator<< ( int numBitsToShift) const

References BigInteger().

◆ operator>>()

BigInteger BigInteger::operator>> ( int numBitsToShift) const

References BigInteger().

◆ operator==()

bool BigInteger::operator== ( const BigInteger & ) const
noexcept

References BigInteger().

◆ operator!=()

bool BigInteger::operator!= ( const BigInteger & ) const
noexcept

References BigInteger().

◆ operator<()

bool BigInteger::operator< ( const BigInteger & ) const
noexcept

References BigInteger().

◆ operator<=()

bool BigInteger::operator<= ( const BigInteger & ) const
noexcept

References BigInteger().

◆ operator>()

bool BigInteger::operator> ( const BigInteger & ) const
noexcept

References BigInteger().

◆ operator>=()

bool BigInteger::operator>= ( const BigInteger & ) const
noexcept

References BigInteger().

◆ compare()

int BigInteger::compare ( const BigInteger & other) const
noexcept

Does a signed comparison of two BigIntegers.

Return values are:

  • 0 if the numbers are the same
  • < 0 if this number is smaller than the other
  • > 0 if this number is bigger than the other

References BigInteger(), and compare().

Referenced by compare().

◆ compareAbsolute()

int BigInteger::compareAbsolute ( const BigInteger & other) const
noexcept

Compares the magnitudes of two BigIntegers, ignoring their signs.

Return values are:

  • 0 if the numbers are the same
  • < 0 if this number is smaller than the other
  • > 0 if this number is bigger than the other

References BigInteger(), and compareAbsolute().

Referenced by compareAbsolute().

◆ divideBy()

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.

References BigInteger(), and divideBy().

Referenced by divideBy().

◆ findGreatestCommonDivisor()

BigInteger BigInteger::findGreatestCommonDivisor ( BigInteger other) const

Returns the largest value that will divide both this value and the argument.

References BigInteger(), and findGreatestCommonDivisor().

Referenced by findGreatestCommonDivisor().

◆ exponentModulo()

void BigInteger::exponentModulo ( const BigInteger & exponent,
const BigInteger & modulus )

Performs a combined exponent and modulo operation.

This BigInteger's value becomes (this ^ exponent) % modulus.

References BigInteger(), and exponentModulo().

Referenced by exponentModulo().

◆ inverseModulo()

void BigInteger::inverseModulo ( const BigInteger & modulus)

Performs an inverse modulo on the value.

i.e. the result is (this ^ -1) mod (modulus).

References BigInteger(), and inverseModulo().

Referenced by inverseModulo().

◆ montgomeryMultiplication()

void BigInteger::montgomeryMultiplication ( const BigInteger & other,
const BigInteger & modulus,
const BigInteger & modulusp,
int k )

Performs the Montgomery Multiplication with modulo.

This object is left containing the result value: ((this * other) * R1) % modulus. To get this result, we need modulus, modulusp and k such as R = 2^k, with modulus * modulusp - R * R1 = GCD(modulus, R) = 1

References BigInteger(), and montgomeryMultiplication().

Referenced by montgomeryMultiplication().

◆ extendedEuclidean()

void BigInteger::extendedEuclidean ( const BigInteger & a,
const BigInteger & b,
BigInteger & xOut,
BigInteger & yOut )

Performs the Extended Euclidean algorithm.

This method will set the xOut and yOut arguments such that (a * xOut) - (b * yOut) = GCD (a, b). On return, this object is left containing the value of the GCD.

References BigInteger(), and extendedEuclidean().

Referenced by extendedEuclidean().

◆ toString()

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.

References toString().

Referenced by toString().

◆ parseString()

void BigInteger::parseString ( StringRef 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.

References parseString().

Referenced by parseString().

◆ toMemoryBlock()

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.

See also
loadFromMemoryBlock

References toMemoryBlock().

Referenced by toMemoryBlock().

◆ loadFromMemoryBlock()

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.

See also
toMemoryBlock

References loadFromMemoryBlock().

Referenced by loadFromMemoryBlock().

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram