OpenShot Library | OpenShotAudio  0.2.2
juce::BigInteger Class Reference

An arbitrarily large integer class. More...

#include <juce_BigInteger.h>

Public Member Functions

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

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.

Definition at line 42 of file juce_BigInteger.h.

Constructor & Destructor Documentation

◆ BigInteger() [1/4]

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.

Definition at line 74 of file juce_BigInteger.cpp.

References getHighestBit().

◆ BigInteger() [2/4]

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.

Definition at line 61 of file juce_BigInteger.cpp.

References getHighestBit().

◆ BigInteger() [3/4]

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.

Definition at line 86 of file juce_BigInteger.cpp.

References getHighestBit().

◆ BigInteger() [4/4]

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

Creates a copy of another BigInteger.

Definition at line 103 of file juce_BigInteger.cpp.

References juce::HeapBlock< ElementType, throwOnFailure >::malloc().

◆ ~BigInteger()

juce::BigInteger::~BigInteger ( )

Destructor.

Definition at line 133 of file juce_BigInteger.cpp.

Member Function Documentation

◆ operator=()

◆ swapWith()

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

Swaps the internal contents of this with another object.

Definition at line 137 of file juce_BigInteger.cpp.

References juce::HeapBlock< ElementType, throwOnFailure >::swapWith().

Referenced by divideBy(), exponentModulo(), extendedEuclidean(), findGreatestCommonDivisor(), findNextClearBit(), and inverseModulo().

◆ 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.

Definition at line 202 of file juce_BigInteger.cpp.

◆ isZero()

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

Returns true if no bits are set.

Definition at line 340 of file juce_BigInteger.cpp.

References getHighestBit().

Referenced by findGreatestCommonDivisor(), isNegative(), loadFromMemoryBlock(), negate(), shiftBits(), and toString().

◆ isOne()

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

Returns true if the value is 1.

Definition at line 345 of file juce_BigInteger.cpp.

References getHighestBit().

Referenced by exponentModulo(), inverseModulo(), and loadFromMemoryBlock().

◆ 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.

Definition at line 208 of file juce_BigInteger.cpp.

◆ 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.

Definition at line 214 of file juce_BigInteger.cpp.

◆ clear()

void juce::BigInteger::clear ( )
noexcept

◆ clearBit()

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

Clears a particular bit in the number.

Definition at line 314 of file juce_BigInteger.cpp.

References getHighestBit().

Referenced by compareAbsolute(), and setBit().

◆ setBit() [1/2]

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

◆ setBit() [2/2]

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

Sets or clears a specified bit.

Definition at line 306 of file juce_BigInteger.cpp.

References clearBit(), and setBit().

◆ 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

Definition at line 325 of file juce_BigInteger.cpp.

References setBit().

Referenced by juce::AudioChannelSet::ambisonic(), juce::AudioChannelSet::discreteChannels(), and montgomeryMultiplication().

◆ insertBit()

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

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

Definition at line 331 of file juce_BigInteger.cpp.

References setBit(), and shiftBits().

◆ 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

Definition at line 221 of file juce_BigInteger.cpp.

References getBitRangeAsInt(), and getHighestBit().

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

Definition at line 239 of file juce_BigInteger.cpp.

Referenced by getBitRange(), and toString().

◆ 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.

Definition at line 265 of file juce_BigInteger.cpp.

References setBit().

Referenced by juce::Random::fillBitsRandomly(), and loadFromMemoryBlock().

◆ 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.

Definition at line 862 of file juce_BigInteger.cpp.

References compareAbsolute(), and isZero().

Referenced by divideBy(), insertBit(), and juce::MixerAudioSource::removeInputSource().

◆ countNumberOfSetBits()

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

Returns the total number of set bits in the value.

Definition at line 369 of file juce_BigInteger.cpp.

Referenced by juce::AudioDeviceManager::setAudioDeviceSetup().

◆ 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.

Definition at line 391 of file juce_BigInteger.cpp.

Referenced by juce::WavAudioFormatReader::getChannelLayout().

◆ 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.

Definition at line 402 of file juce_BigInteger.cpp.

References clear(), compareAbsolute(), getHighestBit(), isNegative(), negate(), setNegative(), and swapWith().

◆ 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.

Definition at line 380 of file juce_BigInteger.cpp.

Referenced by BigInteger(), clearBit(), compareAbsolute(), divideBy(), exponentModulo(), findGreatestCommonDivisor(), findNextClearBit(), getBitRange(), isOne(), isZero(), loadFromMemoryBlock(), juce::Random::nextLargeNumber(), operator=(), and toMemoryBlock().

◆ isNegative()

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

Returns true if the value is less than zero.

See also
setNegative, negate

Definition at line 350 of file juce_BigInteger.cpp.

References isZero().

Referenced by compare(), divideBy(), findNextClearBit(), inverseModulo(), montgomeryMultiplication(), and toString().

◆ setNegative()

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

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

See also
isNegative, negate

Definition at line 355 of file juce_BigInteger.cpp.

Referenced by divideBy(), findNextClearBit(), and parseString().

◆ negate()

void juce::BigInteger::negate ( )
noexcept

Inverts the sign of the number.

See also
isNegative, setNegative

Definition at line 360 of file juce_BigInteger.cpp.

References isZero().

Referenced by extendedEuclidean(), and findNextClearBit().

◆ 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

Definition at line 732 of file juce_BigInteger.cpp.

References compareAbsolute(), and isNegative().

Referenced by compareAbsolute(), and montgomeryMultiplication().

◆ 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

Definition at line 745 of file juce_BigInteger.cpp.

References clear(), clearBit(), compare(), getHighestBit(), and setBit().

Referenced by compare(), divideBy(), exponentModulo(), extendedEuclidean(), findNextClearBit(), inverseModulo(), and shiftBits().

◆ 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.

Definition at line 567 of file juce_BigInteger.cpp.

References clear(), compareAbsolute(), getHighestBit(), isNegative(), setBit(), setNegative(), shiftBits(), and swapWith().

Referenced by findGreatestCommonDivisor(), inverseModulo(), and toString().

◆ findGreatestCommonDivisor()

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

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

Definition at line 887 of file juce_BigInteger.cpp.

References divideBy(), getHighestBit(), isZero(), and swapWith().

Referenced by inverseModulo().

◆ 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.

Definition at line 906 of file juce_BigInteger.cpp.

References compareAbsolute(), extendedEuclidean(), getHighestBit(), isOne(), montgomeryMultiplication(), and swapWith().

◆ inverseModulo()

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

Performs an inverse modulo on the value.

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

Definition at line 1029 of file juce_BigInteger.cpp.

References clear(), compareAbsolute(), divideBy(), findGreatestCommonDivisor(), isNegative(), isOne(), swapWith(), and toString().

◆ 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

Definition at line 972 of file juce_BigInteger.cpp.

References compare(), isNegative(), and setRange().

Referenced by exponentModulo().

◆ 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.

Definition at line 992 of file juce_BigInteger.cpp.

References juce::Array< ElementType, TypeOfCriticalSectionToUse, minimumAllocatedSize >::add(), clear(), compareAbsolute(), juce::Array< ElementType, TypeOfCriticalSectionToUse, minimumAllocatedSize >::getReference(), negate(), juce::Array< ElementType, TypeOfCriticalSectionToUse, minimumAllocatedSize >::size(), and swapWith().

Referenced by exponentModulo().

◆ 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.

Definition at line 1085 of file juce_BigInteger.cpp.

References juce::String::charToString(), divideBy(), getBitRangeAsInt(), isNegative(), isZero(), and juce::String::paddedLeft().

Referenced by inverseModulo().

◆ 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.

Definition at line 1132 of file juce_BigInteger.cpp.

References clear(), juce::CharPointer_UTF8::findEndOfWhitespace(), juce::CharacterFunctions::getHexDigitValue(), setNegative(), and juce::StringRef::text.

Referenced by juce::AudioDeviceManager::initialise().

◆ 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

Definition at line 1180 of file juce_BigInteger.cpp.

References getHighestBit().

Referenced by loadFromMemoryBlock().

◆ 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

Definition at line 1192 of file juce_BigInteger.cpp.

References BigInteger(), juce::Random::fillBitsRandomly(), juce::MemoryBlock::getData(), getHighestBit(), juce::MemoryBlock::getSize(), isOne(), isZero(), juce::ByteOrder::littleEndianInt(), loadFromMemoryBlock(), juce::Random::nextInt(), setBitRangeAsInt(), and toMemoryBlock().

Referenced by loadFromMemoryBlock().


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