JUCE  v6.1.6 (6.0.8-1114)
JUCE API
Looking for a senior C++ dev?
I'm looking for work. Hire me!
juce::BigInteger Class Reference

An arbitrarily large integer class. More...

#include <juce_BigInteger.h>

Collaboration diagram for juce::BigInteger:

Public Member Functions

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

Private Types

enum  { numPreallocatedInts = 4 }
 

Private Member Functions

uint32ensureSize (size_t)
 
uint32getValues () const noexcept
 
void shiftLeft (int bits, int startBit)
 
void shiftRight (int bits, int startBit)
 

Private Attributes

size_t allocatedSize
 
HeapBlock< uint32heapAllocation
 
int highestBit = -1
 
bool negative = false
 
uint32 preallocated [numPreallocatedInts]
 

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.

@tags{Core}

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private
Enumerator
numPreallocatedInts 

Constructor & Destructor Documentation

◆ BigInteger() [1/6]

juce::BigInteger::BigInteger ( )

Creates an empty BigInteger.

◆ BigInteger() [2/6]

juce::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]

juce::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]

juce::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]

juce::BigInteger::BigInteger ( const BigInteger )

Creates a copy of another BigInteger.

◆ BigInteger() [6/6]

juce::BigInteger::BigInteger ( BigInteger &&  )
noexcept

Move constructor.

◆ ~BigInteger()

juce::BigInteger::~BigInteger ( )
default

Destructor.

Member Function Documentation

◆ clear()

void juce::BigInteger::clear ( )
noexcept

Resets the value to 0.

◆ clearBit()

void juce::BigInteger::clearBit ( int  bitNumber)
noexcept

Clears a particular bit in the number.

◆ compare()

int juce::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

◆ compareAbsolute()

int juce::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

◆ countNumberOfSetBits()

int juce::BigInteger::countNumberOfSetBits ( ) const
noexcept

◆ divideBy()

void juce::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.

◆ ensureSize()

uint32* juce::BigInteger::ensureSize ( size_t  )
private

◆ exponentModulo()

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

Performs a combined exponent and modulo operation.

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

◆ extendedEuclidean()

void juce::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.

◆ findGreatestCommonDivisor()

BigInteger juce::BigInteger::findGreatestCommonDivisor ( BigInteger  other) const

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

◆ findNextClearBit()

int juce::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.

◆ findNextSetBit()

int juce::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.

◆ getBitRange()

BigInteger juce::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

◆ getBitRangeAsInt()

uint32 juce::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().

◆ getHighestBit()

int juce::BigInteger::getHighestBit ( ) const
noexcept

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

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

◆ getValues()

uint32* juce::BigInteger::getValues ( ) const
privatenoexcept

◆ insertBit()

void juce::BigInteger::insertBit ( int  bitNumber,
bool  shouldBeSet 
)

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

◆ inverseModulo()

void juce::BigInteger::inverseModulo ( const BigInteger modulus)

Performs an inverse modulo on the value.

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

◆ isNegative()

bool juce::BigInteger::isNegative ( ) const
noexcept

Returns true if the value is less than zero.

See also
setNegative, negate

◆ isOne()

bool juce::BigInteger::isOne ( ) const
noexcept

Returns true if the value is 1.

◆ isZero()

bool juce::BigInteger::isZero ( ) const
noexcept

Returns true if no bits are set.

◆ loadFromMemoryBlock()

void juce::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

◆ montgomeryMultiplication()

void juce::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

◆ negate()

void juce::BigInteger::negate ( )
noexcept

Inverts the sign of the number.

See also
isNegative, setNegative

◆ operator!=()

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

◆ operator%()

BigInteger juce::BigInteger::operator% ( const BigInteger ) const

◆ operator%=()

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

◆ operator&()

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

◆ operator&=()

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

◆ operator*()

BigInteger juce::BigInteger::operator* ( const BigInteger ) const

◆ operator*=()

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

◆ operator+()

BigInteger juce::BigInteger::operator+ ( const BigInteger ) const

◆ operator++() [1/2]

BigInteger& juce::BigInteger::operator++ ( )

◆ operator++() [2/2]

BigInteger juce::BigInteger::operator++ ( int  )

◆ operator+=()

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

◆ operator-() [1/2]

BigInteger juce::BigInteger::operator- ( ) const

◆ operator-() [2/2]

BigInteger juce::BigInteger::operator- ( const BigInteger ) const

◆ operator--() [1/2]

BigInteger& juce::BigInteger::operator-- ( )

◆ operator--() [2/2]

BigInteger juce::BigInteger::operator-- ( int  )

◆ operator-=()

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

◆ operator/()

BigInteger juce::BigInteger::operator/ ( const BigInteger ) const

◆ operator/=()

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

◆ operator<()

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

◆ operator<<()

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

◆ operator<<=()

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

◆ operator<=()

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

◆ operator=() [1/2]

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

Move assignment operator.

◆ operator=() [2/2]

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

Copies another BigInteger onto this one.

◆ operator==()

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

◆ operator>()

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

◆ operator>=()

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

◆ operator>>()

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

◆ operator>>=()

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

◆ operator[]()

bool juce::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.

◆ operator^()

BigInteger juce::BigInteger::operator^ ( const BigInteger ) const

◆ operator^=()

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

◆ operator|()

BigInteger juce::BigInteger::operator| ( const BigInteger ) const

◆ operator|=()

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

◆ parseString()

void juce::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.

◆ setBit() [1/2]

void juce::BigInteger::setBit ( int  bitNumber)

Sets a specified bit to 1.

◆ setBit() [2/2]

void juce::BigInteger::setBit ( int  bitNumber,
bool  shouldBeSet 
)

Sets or clears a specified bit.

◆ setBitRangeAsInt()

void juce::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.

◆ setNegative()

void juce::BigInteger::setNegative ( bool  shouldBeNegative)
noexcept

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

See also
isNegative, negate

◆ setRange()

void juce::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

◆ shiftBits()

void juce::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.

◆ shiftLeft()

void juce::BigInteger::shiftLeft ( int  bits,
int  startBit 
)
private

◆ shiftRight()

void juce::BigInteger::shiftRight ( int  bits,
int  startBit 
)
private

◆ swapWith()

void juce::BigInteger::swapWith ( BigInteger )
noexcept

Swaps the internal contents of this with another object.

◆ toInt64()

int64 juce::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.

◆ toInteger()

int juce::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.

◆ toMemoryBlock()

MemoryBlock juce::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

◆ toString()

String juce::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.

Member Data Documentation

◆ allocatedSize

size_t juce::BigInteger::allocatedSize
private

◆ heapAllocation

HeapBlock<uint32> juce::BigInteger::heapAllocation
private

◆ highestBit

int juce::BigInteger::highestBit = -1
private

◆ negative

bool juce::BigInteger::negative = false
private

◆ preallocated

uint32 juce::BigInteger::preallocated[numPreallocatedInts]
private

The documentation for this class was generated from the following file: