Namespaces | Classes | Typedefs | Functions | Variables

boost::xint Namespace Reference

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

There are only a few functions here because almost all mathematical primitives are implemented as operators.

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

Note that these only convert an integer type to something else. See the integer constructors for conversions the other way.

template<typename 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.

See also:
operator%(integer_t, integer_t)
integer_t::operator%=
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

The nothrow versions of these functions return false instead of throwing.

See also:
compare(integer_t, integer_t, bool)
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

These act exactly the same as for the built-in integer types.

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.

Detailed Description

All the types and functions of the library are within this namespace.


Typedef Documentation

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.

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.

Parameters:
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.
Returns:
If this function returns, the exception will be thrown as normal, or (if BOOST_XINT_NO_EXCEPTIONS is used) abort() will be called.
See also:
The on_exception Function And -fno-exceptions Support

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.

See also:
The XInt Random Number System
integer_t::random_by_size
integer_t::random_prime

Definition at line 97 of file random.hpp.


Function Documentation

template<... >
integer_t<...> boost::xint::abs ( const integer_t<...>  n  ) 

Returns the absolute value of an integer.

  • Complexity: O(1)
Parameters:
[in] n The integer to operate on.
Returns:
If n is zero or positive, returns n. Otherwise returns -n.

Definition at line 1215 of file integer.hpp.

template<... >
void boost::xint::clearbit ( integer_t<...> &  n,
bitsize_t  bit 
)

Sets a specific bit in an integer to zero.

  • Complexity: O(1)
Parameters:
[in,out] n The integer to operate on.
[in] bit The zero-based index of the bit to change.
Returns:
Nothing.

Definition at line 1605 of file integer.hpp.

template<... >
int boost::xint::compare ( const integer_t<...>  n1,
const integer_t<...>  n2,
bool  ignoresign = false 
)

Compare two integer objects.

  • Complexity: varies between O(n) if the numbers are equal (the worst case), down to O(1) if they have different signs or wildly different magnitudes.
Parameters:
[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.
Returns:
-1 if 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.

template<... >
integer_t<...> boost::xint::difference ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Calculate the absolute difference between two integers.

  • Complexity: O(n)
Parameters:
[in] n1,n2 The integers to operate on.
Returns:
The absolute difference between the parameters.

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.

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

  • Complexity: O(n2)
Parameters:
[in] dividend,divisor The integers to operate on.
Returns:
A 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:
exceptions::divide_by_zero if d2 is zero.
See also:
operator/(integer_t, integer_t)
operator%(integer_t, integer_t)
integer_t::operator/=
integer_t::operator%=

Definition at line 1253 of file integer.hpp.

template<... >
integer_t<...> boost::xint::gcd ( const integer_t<...>  num1,
const integer_t<...>  num2 
)

Calculate the Greatest Common Denominator of two integers.

  • Complexity: O((log n)3)
Parameters:
[in] num1,num2 The integers to operate on.
Returns:
The greatest common denominator of the two integers, which will always be a positive number.

Definition at line 2242 of file integer.hpp.

template<... >
bool boost::xint::getbit ( const integer_t<...>  n,
bitsize_t  bit 
)

Queries the value of a particular bit in an integer.

  • Complexity: O(1)
Parameters:
[in] n The integer to query.
[in] bit The zero-based index of the bit you're asking about.
Returns:
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.

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

  • Complexity: O(1)
Parameters:
[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).
Returns:
The zero-based index of the highest one-bit in the integer, or 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.

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

  • Complexity: O((log n)3)
Parameters:
[in] n The number to calculate the inverse of.
[in] modulus The modulus to use.
Returns:
The modular inverse of n in modulus. If n has no modular inverse in modulus, returns zero.
Exceptions:
exceptions::invalid_modulus if the modulus is less than one.

Definition at line 1824 of file integer.hpp.

template<... >
int boost::xint::is_prime ( const integer_t<...>  n,
callback_t  callback = no_callback 
)

Tests an integer for primality.

  • Complexity: Varies
Parameters:
[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.
Returns:
1 if 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:
exceptions::invalid_argument if n is less than 2.
Remarks:
This function uses the Rabin-Miller probabilistic primality test. There is an infinitesimally small chance that it will think that a composite number is actually prime, but that is so small that it can be ignored for most practical purposes. If even that chance is too much, you can run this function over the number several times, it will use different randomly-selected "witness" numbers each time.
See also:
Generating Prime Numbers

Definition at line 1878 of file integer.hpp.

template<... >
integer_t<...> boost::xint::lcm ( const integer_t<...>  num1,
const integer_t<...>  num2 
)

Calculate the Least Common Multiple of two integers.

  • Complexity: O((log n)3 + n2)
Parameters:
[in] num1,num2 The integers to operate on.
Returns:
The least common multiple of the two integers. If either integer is zero, then the return value will be zero, by convention; in all other cases, the return value will be a positive number.

Definition at line 2276 of file integer.hpp.

template<... >
size_t boost::xint::log2 ( const integer_t<...>  n  ) 

Get the log2 value of an integer.

  • Complexity: O(1)
Parameters:
[in] n The integer to operate on.
Returns:
The integer log2 value of the integer. The nothrow version returns zero instead of throwing.
Remarks:
pow2(log2(n)-1) will give you an integer with the highest set bit of n, assuming that n is non-zero.
Similar to the highestbit function.

Definition at line 2344 of file integer.hpp.

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

  • Complexity: O(1)
Parameters:
[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).
Returns:
The zero-based index of the lowest one-bit in the integer, or 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.

template<... >
integer_t<...> boost::xint::mulmod ( const integer_t<...>  n,
const integer_t<...>  by,
const integer_t<...>  modulus 
)

Get the modular product of two integers.

  • Complexity: O(2n2)
Parameters:
[in] n,by The integers to multiply.
[in] modulus The modulus.
Returns:
The result of n * by % modulus.
Remarks:
This is purely a convenience function, to make it easier to write self-documenting code. It does not provide any additional efficiency over writing out the calculation.

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.

Parameters:
fn The function to set as the exception handler, of type on_exception_t. If not specified, removes any previous exception handler.
Note:
This function, and the underlying exception handler system, is not thread-safe. It is your responsibility to ensure that no other threads are using the library while calling this function.
See also:
The on_exception Function And -fno-exceptions Support

Definition at line 39 of file exceptions.hpp.

template<... >
bool boost::xint::operator!= ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Definition at line 1976 of file integer.hpp.

template<... >
integer_t<...> boost::xint::operator% ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Get the remainder after dividing a number by another.

  • Complexity: O(n2)
Parameters:
[in] n1 The dividend.
[in] n2 The divisor.
Returns:
The remainder after dividing n1 by n2.

Definition at line 2126 of file integer.hpp.

template<... >
integer_t<...> boost::xint::operator& ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Combine two integers using a bitwise AND operation.

  • Complexity: O(n)
Parameters:
[in] n1,n2 The integers to operate on.
Returns:
A positive integer with all bits that are set in both parameters turned on.

Definition at line 2154 of file integer.hpp.

template<... >
integer_t<...> boost::xint::operator* ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Calculate the product of two integers.

  • Complexity: O(n2)
Parameters:
[in] n1,n2 The integers to operate on.
Returns:
The product of the parameters.
Remarks:
Automatically uses the more-efficient squaring algorithm if it can trivially detect that the two parameters are copies of the same number.

Definition at line 2069 of file integer.hpp.

template<... >
integer_t<...> boost::xint::operator+ ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Calculate the sum of two integers.

  • Complexity: O(n)
Parameters:
[in] n1,n2 The integers to add.
Returns:
The sum of the parameters.

Definition at line 2007 of file integer.hpp.

template<... >
integer_t<...> boost::xint::operator- ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Calculate the difference between two integers.

  • Complexity: O(n)
Parameters:
[in] n1,n2 The integers to operate on.
Returns:
The difference between the parameters.

Definition at line 2034 of file integer.hpp.

template<... >
integer_t<...> boost::xint::operator/ ( const integer_t<...>  dividend,
const integer_t<...>  divisor 
)

Calculate how many dividends would fit into divisor.

  • Complexity: O(n2)
Parameters:
[in] dividend,divisor The integers to operate on.
Returns:
The integer value of dividend divided by divisor.
Exceptions:
exceptions::divide_by_zero if divisor is zero.

Definition at line 2098 of file integer.hpp.

template<... >
bool boost::xint::operator< ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Definition at line 1901 of file integer.hpp.

template<typename charT , typename traits , ... >
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.

template<... >
bool boost::xint::operator<= ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Definition at line 1931 of file integer.hpp.

template<... >
bool boost::xint::operator== ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Definition at line 1961 of file integer.hpp.

template<... >
bool boost::xint::operator> ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Definition at line 1916 of file integer.hpp.

template<... >
bool boost::xint::operator>= ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Definition at line 1946 of file integer.hpp.

template<typename charT , typename traits , ... >
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.

template<... >
integer_t<...> boost::xint::operator^ ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Combine two integers using a bitwise XOR operation.

  • Complexity: O(n)
Parameters:
[in] n1,n2 The integers to operate on.
Returns:
A positive integer with all bits that are set in either parameter, but not both, turned on.

Definition at line 2210 of file integer.hpp.

template<... >
integer_t<...> boost::xint::operator| ( const integer_t<...>  n1,
const integer_t<...>  n2 
)

Combine two integers using a bitwise OR operation.

  • Complexity: O(n)
Parameters:
[in] n1,n2 The integers to operate on.
Returns:
A positive integer with all bits that are set in either parameter turned on.

Definition at line 2182 of file integer.hpp.

template<... >
integer_t<...> boost::xint::pow ( const integer_t<...>  n,
const integer_t<...>  e 
)

Calculate the value of ne

  • Complexity: O(nn2ne)
Parameters:
[in] n,e The integers to operate on.
Returns:
n to the power of e.

Definition at line 1353 of file integer.hpp.

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

  • Complexity: O(nn2ne)
Parameters:
[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.
Returns:
The result of pow(n, exponent) % modulus.
Exceptions:
exceptions::invalid_modulus if the modulus is less than one.
Remarks:
Because this function keeps the intermediate results small, it is far faster and uses far less memory than a naive implementation.
In addition, this function will use the Montgomery Reduction internally, if the modulus is an odd number (and if no_monty isn't set), which is almost always faster than the non-Montgomery method.

Definition at line 1787 of file integer.hpp.

template<... >
void boost::xint::setbit ( integer_t<...> &  n,
bitsize_t  bit 
)

Sets a specific bit in an integer to one.

  • Complexity: O(1)
Parameters:
[in,out] n The integer to operate on.
[in] bit The zero-based index of the bit to change.
Returns:
Nothing.

Definition at line 1581 of file integer.hpp.

template<... >
integer_t<...> boost::xint::sqrmod ( const integer_t<...>  n,
const integer_t<...>  modulus 
)

Get the modular product of squaring an integer.

  • Complexity: O(2n2)
Parameters:
[in] n The integer to square.
[in] modulus The modulus.
Returns:
The result of square(n) % modulus.
Remarks:
This is purely a convenience function, to make it easier to write self-documenting code. It does not provide any additional efficiency over writing out the calculation.

Definition at line 1742 of file integer.hpp.

template<... >
integer_t<...> boost::xint::square ( const integer_t<...>  n  ) 

Calculate the value of n2

  • Complexity: O(n(n+1)/2)
Parameters:
[in] n The integer to operate on.
Returns:
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.

template<... >
integer_t<...> boost::xint::square_root ( const integer_t<...>  n  ) 

Calculate the square root of an integer.

  • Complexity: O(n2)
Parameters:
[in] n The value to operate on.
Returns:
The largest integer that, when squared, is less than or equal to n. If n is not a perfect square, then this value will not be the exact square root.
Exceptions:
exceptions::cannot_represent if n is negative.

Definition at line 1389 of file integer.hpp.

template<... >
void boost::xint::swap ( integer_t<...> &  left,
integer_t<...> &  right 
) [inline]

Definition at line 2510 of file integer.hpp.

template<typename T , ... >
T boost::xint::to ( const integer_t<...>  n  ) 

Converts an xint::integer type to a built-in integer type.

Parameters:
[in] n The integer to convert.
Template Parameters:
T The type to convert it to.
Returns:
The numeric value of n, converted to the specified type.
Exceptions:
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.

template<... >
xint::binary_t boost::xint::to_binary ( const integer_t<...>  n,
bitsize_t  bits = 0 
)

Creates a binary representation of an integer.

  • Complexity: O(n)
Parameters:
[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.
Returns:
An xint::binary_t containing the binary representation, lowest byte first. The nothrow version returns an empty binary_t object instead of throwing.
Exceptions:
exceptions::invalid_argument if bits is greater than the number of bits in an unsigned character.
Note:
This function only stores the absolute value of n; if you need the sign, you must store it separately.
Remarks:
A binary representation is sometimes desirable for persistent storage or transmission, as it is more space-efficient than a string representation.
See also:
integer_t::integer_t(xint::binary_t, bitsize_t bits)

Definition at line 1528 of file integer.hpp.

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

template<typename charT , ... >
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.

  • Complexity: O(n2)
Template Parameters:
charT The type of string to create.
Parameters:
[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.
Returns:
The string value of n. The nothrow version returns the string "#NaN#" if n is a Not-a-Number, and an empty string instead of throwing.
Exceptions:
exceptions::invalid_base if base is less than two or greater than 36.
Remarks:
This is the function that's called when you ask the library to write an integer to a stream, but it's more flexible because you can specify any base between 2 and 36. (Streams only allow base-8, base-10, or base-16.)

Definition at line 1465 of file integer.hpp.

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


Variable Documentation

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)