All the types and functions of the library are within this namespace. More...
Namespaces | |
namespace | detail |
The items within this namespace are meant for internal use only. | |
namespace | exceptions |
Every exception the library throws is from this namespace. | |
namespace | options |
Options for the integer_t template are stored in this namespace. | |
Classes | |
class | base_divide_t |
Holds the results from a division-with-remainder call. More... | |
class | default_random_generator |
A convenience class for general-purpose (insecure) random numbers. More... | |
class | integer_t |
The integer_t class template. More... | |
Typedefs | |
typedef std::vector< unsigned char > | binary_t |
The format for the binary representations of integers. | |
typedef std::size_t | bitsize_t |
This makes it easier to tell when an item is measured in bits. | |
typedef boost::function< bool()> | callback_t |
A callback function takes no parameters and returns a bool. | |
typedef integer_t | integer |
The default arbitrary-length integer type. | |
typedef integer_t < options::nothrow > | nothrow_integer |
The default arbitrary-length nothrow_integer type. | |
typedef boost::function< void(const char *file, std::size_t line, const std::exception &e) | on_exception_t ) |
The function type for on_exception. | |
typedef detail::strong_random_generator_t | strong_random_generator |
A convenience class for cryptographically-secure random numbers. | |
Functions | |
void | on_exception (on_exception_t fn=on_exception_t()) |
Sets or resets the global exception handler for the library. | |
template<... > | |
void | swap (integer_t<...> &left, integer_t<...> &right) |
Mathematical primitives | |
template<... > | |
integer_t<...> | abs (const integer_t<...> n) |
Returns the absolute value of an integer. | |
template<... > | |
integer_t<...>::divide_t | divide (const integer_t<...> dividend, const integer_t<...> divisor) |
Calculate how many dividends would fit into divisor , with the remainder. | |
template<... > | |
integer_t<...> | difference (const integer_t<...> n1, const integer_t<...> n2) |
Calculate the absolute difference between two integers. | |
Powers and roots | |
| |
template<... > | |
integer_t<...> | square (const integer_t<...> n) |
Calculate the value of n2 | |
template<... > | |
integer_t<...> | pow (const integer_t<...> n, const integer_t<...> e) |
Calculate the value of ne | |
template<... > | |
integer_t<...> | square_root (const integer_t<...> n) |
Calculate the square root of an integer. | |
Conversion functions | |
template<typename T , ... > | |
T | to (const integer_t<...> n) |
Converts an xint::integer type to a built-in integer type. | |
template<typename charT , ... > | |
std::basic_string< charT > | to_stringtype (const integer_t<...> n, std::size_t base=10, bool uppercase=false) |
Creates a string representation of the specified integer. | |
template<... > | |
std::string | to_string (const integer_t<...> n, std::size_t base=10, bool uppercase=false) |
A shorthand function that calls to_stringtype<char>. | |
template<... > | |
std::wstring | to_wstring (const integer_t<...> n, std::size_t base=10, bool uppercase=false) |
A shorthand function that calls to_stringtype<wchar_t>. | |
template<... > | |
xint::binary_t | to_binary (const integer_t<...> n, bitsize_t bits=0) |
Creates a binary representation of an integer. | |
Bit-manipulation functions | |
template<... > | |
bool | getbit (const integer_t<...> n, bitsize_t bit) |
Queries the value of a particular bit in an integer. | |
template<... > | |
void | setbit (integer_t<...> &n, bitsize_t bit) |
Sets a specific bit in an integer to one. | |
template<... > | |
void | clearbit (integer_t<...> &n, bitsize_t bit) |
Sets a specific bit in an integer to zero. | |
template<... > | |
bitsize_t | lowestbit (const integer_t<...> n, bitsize_t return_if_zero=0) |
Obtains the index of the lowest bit in an integer with a value of one. | |
template<... > | |
bitsize_t | highestbit (const integer_t<...> n, bitsize_t return_if_zero=0) |
Obtains the index of the highest bit in an integer with a value of one. | |
Modular math functions | |
A common reason to use a large-integer library is to implement public-key encryption, and the algorithms for such encryption often use modular math.
| |
template<... > | |
integer_t<...> | mulmod (const integer_t<...> n, const integer_t<...> by, const integer_t<...> modulus) |
Get the modular product of two integers. | |
template<... > | |
integer_t<...> | sqrmod (const integer_t<...> n, const integer_t<...> modulus) |
Get the modular product of squaring an integer. | |
template<... > | |
integer_t<...> | powmod (const integer_t<...> n, const integer_t<...> exponent, const integer_t<...> modulus, bool no_monty=false) |
Get the result of ne % m , keeping the intermediate results (relatively) small. | |
template<... > | |
integer_t<...> | invmod (const integer_t<...> n, const integer_t<...> modulus) |
Get the modular inverse of a number in a modulus, if there is one. | |
Random and prime number functions | |
template<... > | |
int | is_prime (const integer_t<...> n, callback_t callback=no_callback) |
Tests an integer for primality. | |
Comparison Operators | |
template<... > | |
bool | operator< (const integer_t<...> n1, const integer_t<...> n2) |
template<... > | |
bool | operator> (const integer_t<...> n1, const integer_t<...> n2) |
template<... > | |
bool | operator<= (const integer_t<...> n1, const integer_t<...> n2) |
template<... > | |
bool | operator>= (const integer_t<...> n1, const integer_t<...> n2) |
template<... > | |
bool | operator== (const integer_t<...> n1, const integer_t<...> n2) |
template<... > | |
bool | operator!= (const integer_t<...> n1, const integer_t<...> n2) |
Mathematical and Bitwise Operators | |
template<... > | |
integer_t<...> | operator+ (const integer_t<...> n1, const integer_t<...> n2) |
Calculate the sum of two integers. | |
template<... > | |
integer_t<...> | operator- (const integer_t<...> n1, const integer_t<...> n2) |
Calculate the difference between two integers. | |
template<... > | |
integer_t<...> | operator* (const integer_t<...> n1, const integer_t<...> n2) |
Calculate the product of two integers. | |
template<... > | |
integer_t<...> | operator/ (const integer_t<...> dividend, const integer_t<...> divisor) |
Calculate how many dividends would fit into divisor . | |
template<... > | |
integer_t<...> | operator% (const integer_t<...> n1, const integer_t<...> n2) |
Get the remainder after dividing a number by another. | |
template<... > | |
integer_t<...> | operator& (const integer_t<...> n1, const integer_t<...> n2) |
Combine two integers using a bitwise AND operation. | |
template<... > | |
integer_t<...> | operator| (const integer_t<...> n1, const integer_t<...> n2) |
Combine two integers using a bitwise OR operation. | |
template<... > | |
integer_t<...> | operator^ (const integer_t<...> n1, const integer_t<...> n2) |
Combine two integers using a bitwise XOR operation. | |
Miscellaneous functions | |
template<... > | |
integer_t<...> | gcd (const integer_t<...> num1, const integer_t<...> num2) |
Calculate the Greatest Common Denominator of two integers. | |
template<... > | |
integer_t<...> | lcm (const integer_t<...> num1, const integer_t<...> num2) |
Calculate the Least Common Multiple of two integers. | |
template<... > | |
int | compare (const integer_t<...> n1, const integer_t<...> n2, bool ignoresign=false) |
Compare two integer objects. | |
template<... > | |
size_t | log2 (const integer_t<...> n) |
Get the log2 value of an integer. | |
Stream input/output functions | |
template<typename charT , typename traits , ... > | |
std::basic_ostream< charT, traits > & | operator<< (std::basic_ostream< charT, traits > &out, const integer_t<...> n) |
template<typename charT , typename traits , ... > | |
std::basic_istream< charT, traits > & | operator>> (std::basic_istream< charT, traits > &in, integer_t<...> &n) |
Variables | |
const std::size_t | autobase = (std::numeric_limits<size_t>::max)() |
A value that can be used for the 'base' parameter of the string-to-integer constructor functions. | |
const callback_t | no_callback |
An empty callback_t, used as a default parameter for some functions. |
All the types and functions of the library are within this namespace.
typedef std::vector<unsigned char> boost::xint::binary_t |
The format for the binary representations of integers.
Definition at line 98 of file basic_types_and_includes.hpp.
typedef std::size_t boost::xint::bitsize_t |
This makes it easier to tell when an item is measured in bits.
Definition at line 105 of file basic_types_and_includes.hpp.
typedef boost::function<bool ()> boost::xint::callback_t |
A callback function takes no parameters and returns a bool.
Definition at line 92 of file basic_types_and_includes.hpp.
typedef integer_t boost::xint::integer |
The default arbitrary-length integer type.
This uses the default parameters for the integer_t template.
Definition at line 2520 of file integer.hpp.
The default arbitrary-length nothrow_integer type.
This uses the default parameters for the integer_t template, other than the nothrow
one.
Definition at line 2527 of file integer.hpp.
typedef boost::function<void (const char *file, std::size_t line, const std::exception& e) boost::xint::on_exception_t) |
The function type for on_exception.
file | The source code file that the exception originated from, as reported by the __FILE__ macro. | |
line | The line (in that file) where the exception originated, as reported by the __LINE__ macro. | |
e | The exception that was thrown. |
abort()
will be called.Definition at line 89 of file basic_types_and_includes.hpp.
typedef detail::strong_random_generator_t boost::xint::strong_random_generator |
A convenience class for cryptographically-secure random numbers.
Definition at line 97 of file random.hpp.
integer_t<...> boost::xint::abs | ( | const integer_t<...> | n | ) |
Returns the absolute value of an integer.
[in] | n | The integer to operate on. |
n
is zero or positive, returns n
. Otherwise returns -n
. Definition at line 1215 of file integer.hpp.
void boost::xint::clearbit | ( | integer_t<...> & | n, | |
bitsize_t | bit | |||
) |
Sets a specific bit in an integer to zero.
[in,out] | n | The integer to operate on. |
[in] | bit | The zero-based index of the bit to change. |
Definition at line 1605 of file integer.hpp.
int boost::xint::compare | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2, | |||
bool | ignoresign = false | |||
) |
Compare two integer objects.
[in] | n1,n2 | The integers to compare. |
[in] | ignoresign | If true , the absolute values of n1 and n2 are compared, instead of their signed values. Used internally. |
n1
< n2
; zero if n1
== n2
, or 1 if n1
> n2
. The nothrow version returns 2 instead of throwing. Definition at line 2312 of file integer.hpp.
integer_t<...> boost::xint::difference | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Calculate the absolute difference between two integers.
[in] | n1,n2 | The integers to operate on. |
This function is especially useful when using unsigned types, because it's faster than doing a comparison to see which integer is larger before doing a subtraction.
Definition at line 1297 of file integer.hpp.
integer_t<...>::divide_t boost::xint::divide | ( | const integer_t<...> | dividend, | |
const integer_t<...> | divisor | |||
) |
Calculate how many dividends
would fit into divisor
, with the remainder.
[in] | dividend,divisor | The integers to operate on. |
divide_t
class containing the quotient and remainder of dividend
divided by divisor
. The nothrow version returns a divide_t
with both items set to Not-a-Number instead of throwing.exceptions::divide_by_zero | if d2 is zero. |
Definition at line 1253 of file integer.hpp.
integer_t<...> boost::xint::gcd | ( | const integer_t<...> | num1, | |
const integer_t<...> | num2 | |||
) |
Calculate the Greatest Common Denominator of two integers.
[in] | num1,num2 | The integers to operate on. |
Definition at line 2242 of file integer.hpp.
bool boost::xint::getbit | ( | const integer_t<...> | n, | |
bitsize_t | bit | |||
) |
Queries the value of a particular bit in an integer.
[in] | n | The integer to query. |
[in] | bit | The zero-based index of the bit you're asking about. |
true
if the specified bit is set (has a value of one), false
if it is clear. The nothrow version returns false
instead of throwing. Definition at line 1558 of file integer.hpp.
bitsize_t boost::xint::highestbit | ( | const integer_t<...> | n, | |
bitsize_t | return_if_zero = 0 | |||
) |
Obtains the index of the highest bit in an integer with a value of one.
[in] | n | The integer to query. |
[in] | return_if_zero | The value to return if n is zero (as there is no correct answer in that case). |
return_if_zero
if the integer contains no set bits. The nothrow version returns return_if_zero
instead of throwing. Definition at line 1662 of file integer.hpp.
integer_t<...> boost::xint::invmod | ( | const integer_t<...> | n, | |
const integer_t<...> | modulus | |||
) |
Get the modular inverse of a number in a modulus, if there is one.
[in] | n | The number to calculate the inverse of. |
[in] | modulus | The modulus to use. |
n
in modulus
. If n
has no modular inverse in modulus
, returns zero.exceptions::invalid_modulus | if the modulus is less than one. |
Definition at line 1824 of file integer.hpp.
int boost::xint::is_prime | ( | const integer_t<...> | n, | |
callback_t | callback = no_callback | |||
) |
Tests an integer for primality.
[in] | n | The number to test. |
[in] | callback | An optional function that will be called regularly during the operation. If it returns false , the function will immediately return. |
n
seems to be prime, 0 if it isn't, or -1 if the provided callback function cancelled the operation. The nothrow version returns -2 instead of throwing.exceptions::invalid_argument | if n is less than 2. |
Definition at line 1878 of file integer.hpp.
integer_t<...> boost::xint::lcm | ( | const integer_t<...> | num1, | |
const integer_t<...> | num2 | |||
) |
Calculate the Least Common Multiple of two integers.
[in] | num1,num2 | The integers to operate on. |
Definition at line 2276 of file integer.hpp.
size_t boost::xint::log2 | ( | const integer_t<...> | n | ) |
Get the log2 value of an integer.
[in] | n | The integer to operate on. |
n
, assuming that n
is non-zero.Definition at line 2344 of file integer.hpp.
bitsize_t boost::xint::lowestbit | ( | const integer_t<...> | n, | |
bitsize_t | return_if_zero = 0 | |||
) |
Obtains the index of the lowest bit in an integer with a value of one.
[in] | n | The integer to query. |
[in] | return_if_zero | The value to return if n is zero (as there is no correct answer in that case). |
return_if_zero
if the integer contains no set bits. The nothrow version returns return_if_zero
instead of throwing. Definition at line 1633 of file integer.hpp.
integer_t<...> boost::xint::mulmod | ( | const integer_t<...> | n, | |
const integer_t<...> | by, | |||
const integer_t<...> | modulus | |||
) |
Get the modular product of two integers.
[in] | n,by | The integers to multiply. |
[in] | modulus | The modulus. |
n
* by
% modulus
.Definition at line 1703 of file integer.hpp.
void boost::xint::on_exception | ( | on_exception_t | fn = on_exception_t() |
) | [inline] |
Sets or resets the global exception handler for the library.
fn | The function to set as the exception handler, of type on_exception_t. If not specified, removes any previous exception handler. |
Definition at line 39 of file exceptions.hpp.
bool boost::xint::operator!= | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Definition at line 1976 of file integer.hpp.
integer_t<...> boost::xint::operator% | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Get the remainder after dividing a number by another.
[in] | n1 | The dividend. |
[in] | n2 | The divisor. |
n1
by n2
. Definition at line 2126 of file integer.hpp.
integer_t<...> boost::xint::operator& | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Combine two integers using a bitwise AND
operation.
[in] | n1,n2 | The integers to operate on. |
Definition at line 2154 of file integer.hpp.
integer_t<...> boost::xint::operator* | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Calculate the product of two integers.
[in] | n1,n2 | The integers to operate on. |
Definition at line 2069 of file integer.hpp.
integer_t<...> boost::xint::operator+ | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Calculate the sum of two integers.
[in] | n1,n2 | The integers to add. |
Definition at line 2007 of file integer.hpp.
integer_t<...> boost::xint::operator- | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Calculate the difference between two integers.
[in] | n1,n2 | The integers to operate on. |
Definition at line 2034 of file integer.hpp.
integer_t<...> boost::xint::operator/ | ( | const integer_t<...> | dividend, | |
const integer_t<...> | divisor | |||
) |
Calculate how many dividends
would fit into divisor
.
[in] | dividend,divisor | The integers to operate on. |
dividend
divided by divisor
.exceptions::divide_by_zero | if divisor is zero. |
Definition at line 2098 of file integer.hpp.
bool boost::xint::operator< | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Definition at line 1901 of file integer.hpp.
std::basic_ostream<charT,traits>& boost::xint::operator<< | ( | std::basic_ostream< charT, traits > & | out, | |
const integer_t<...> | n | |||
) | [inline] |
Definition at line 2469 of file integer.hpp.
bool boost::xint::operator<= | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Definition at line 1931 of file integer.hpp.
bool boost::xint::operator== | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Definition at line 1961 of file integer.hpp.
bool boost::xint::operator> | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Definition at line 1916 of file integer.hpp.
bool boost::xint::operator>= | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Definition at line 1946 of file integer.hpp.
std::basic_istream<charT,traits>& boost::xint::operator>> | ( | std::basic_istream< charT, traits > & | in, | |
integer_t<...> & | n | |||
) | [inline] |
Definition at line 2478 of file integer.hpp.
integer_t<...> boost::xint::operator^ | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Combine two integers using a bitwise XOR
operation.
[in] | n1,n2 | The integers to operate on. |
Definition at line 2210 of file integer.hpp.
integer_t<...> boost::xint::operator| | ( | const integer_t<...> | n1, | |
const integer_t<...> | n2 | |||
) |
Combine two integers using a bitwise OR
operation.
[in] | n1,n2 | The integers to operate on. |
Definition at line 2182 of file integer.hpp.
integer_t<...> boost::xint::pow | ( | const integer_t<...> | n, | |
const integer_t<...> | e | |||
) |
Calculate the value of ne
[in] | n,e | The integers to operate on. |
n
to the power of e
. Definition at line 1353 of file integer.hpp.
integer_t<...> boost::xint::powmod | ( | const integer_t<...> | n, | |
const integer_t<...> | exponent, | |||
const integer_t<...> | modulus, | |||
bool | no_monty = false | |||
) |
Get the result of ne
% m
, keeping the intermediate results (relatively) small.
[in] | n | The integer to exponentiate. |
[in] | exponent | The exponent. |
[in] | modulus | The modulus. |
[in] | no_monty | Used for testing, leave false for normal operation. |
pow
(n, exponent
) % modulus
.exceptions::invalid_modulus | if the modulus is less than one. |
no_monty
isn't set), which is almost always faster than the non-Montgomery method. Definition at line 1787 of file integer.hpp.
void boost::xint::setbit | ( | integer_t<...> & | n, | |
bitsize_t | bit | |||
) |
Sets a specific bit in an integer to one.
[in,out] | n | The integer to operate on. |
[in] | bit | The zero-based index of the bit to change. |
Definition at line 1581 of file integer.hpp.
integer_t<...> boost::xint::sqrmod | ( | const integer_t<...> | n, | |
const integer_t<...> | modulus | |||
) |
Get the modular product of squaring an integer.
[in] | n | The integer to square. |
[in] | modulus | The modulus. |
square(n)
% modulus
.Definition at line 1742 of file integer.hpp.
integer_t<...> boost::xint::square | ( | const integer_t<...> | n | ) |
Calculate the value of n2
[in] | n | The integer to operate on. |
n
times n
.The repetitive nature of the input (multiplying a number by itself) allows this function to use a more-efficient algorithm than standard multiplication.
Definition at line 1326 of file integer.hpp.
integer_t<...> boost::xint::square_root | ( | const integer_t<...> | n | ) |
Calculate the square root of an
integer.
[in] | n | The value to operate on. |
n
. If n
is not a perfect square, then this value will not be the exact square root.exceptions::cannot_represent | if n is negative. |
Definition at line 1389 of file integer.hpp.
void boost::xint::swap | ( | integer_t<...> & | left, | |
integer_t<...> & | right | |||
) | [inline] |
Definition at line 2510 of file integer.hpp.
T boost::xint::to | ( | const integer_t<...> | n | ) |
Converts an xint::integer type to a built-in integer type.
[in] | n | The integer to convert. |
T | The type to convert it to. |
n
, converted to the specified type.exceptions::too_big | if n would not fit into the specified type. |
This function provides the most efficient means of converting to a built-in integral type. The nothrow version returns T(0) instead of throwing.
Definition at line 1429 of file integer.hpp.
xint::binary_t boost::xint::to_binary | ( | const integer_t<...> | n, | |
bitsize_t | bits = 0 | |||
) |
Creates a binary representation of an integer.
[in] | n | The integer to convert. |
[in] | bits | The number of bits to pack into each character (for portability with systems that might not have characters with the same number of bits), or zero (the default) to assume the number of bits in the current system's unsigned character type. |
xint::binary_t
containing the binary representation, lowest byte first. The nothrow version returns an empty binary_t
object instead of throwing.exceptions::invalid_argument | if bits is greater than the number of bits in an unsigned character. |
n
; if you need the sign, you must store it separately.Definition at line 1528 of file integer.hpp.
std::string boost::xint::to_string | ( | const integer_t<...> | n, | |
std::size_t | base = 10 , |
|||
bool | uppercase = false | |||
) |
A shorthand function that calls to_stringtype<char>.
Definition at line 1484 of file integer.hpp.
std::basic_string<charT> boost::xint::to_stringtype | ( | const integer_t<...> | n, | |
std::size_t | base = 10 , |
|||
bool | uppercase = false | |||
) |
Creates a string representation of the specified integer.
charT | The type of string to create. |
[in] | n | The integer to convert. |
[in] | base | The base, between 2 and 36 inclusive, to convert it to. Defaults to base 10. |
[in] | uppercase | Whether to make alphabetic characters (for bases greater than ten) uppercase or not. Defaults to false . |
n
. The nothrow version returns the string "#NaN#" if n
is a Not-a-Number, and an empty string instead of throwing.exceptions::invalid_base | if base is less than two or greater than 36. |
Definition at line 1465 of file integer.hpp.
std::wstring boost::xint::to_wstring | ( | const integer_t<...> | n, | |
std::size_t | base = 10 , |
|||
bool | uppercase = false | |||
) |
A shorthand function that calls to_stringtype<wchar_t>.
Definition at line 1494 of file integer.hpp.
const std::size_t boost::xint::autobase = (std::numeric_limits<size_t>::max)() |
A value that can be used for the 'base' parameter of the string-to-integer constructor functions.
Definition at line 102 of file basic_types_and_includes.hpp.
An empty callback_t, used as a default parameter for some functions.
Definition at line 95 of file basic_types_and_includes.hpp.
© Copyright Chad Nelson, 2010-2011. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)