Home / fwnbi::basic_integer
template <
    size_t Bits,
    class  DigitT,
    bool   Signed
> class basic_integer;

Several aliases

template <size_t Bits, class DigitT = /* biggest digit type */<Bits>>
using uintN_t = basic_integer<Bits, DigitT, false>;
template <size_t Bits, class DigitT = /* biggest digit type */<Bits>>
using  intN_t = basic_integer<Bits, DigitT,  true>;
using uint128_t  = uintN_t< 128>;
using uint256_t  = uintN_t< 256>;
using uint512_t  = uintN_t< 512>;
using uint1024_t = uintN_t<1024>;

using  int128_t  =  intN_t< 128>;
using  int256_t  =  intN_t< 256>;
using  int512_t  =  intN_t< 512>;
using  int1024_t =  intN_t<1024>;
Template parameters
Bits count of bits in integer, Bits must be a multiple of bit width of DigitT
DigitT type, used for storage digits of integer, valid types contained in namespace digit
Signed signedness, if equal to true, then integer is signed, else integer is unsigned

The class template emulating semantic of integer with signedness depended on Signed and bit width equal to Bits. Storage digits in contiguous array. Digits order is little i.e. first digit with index 0 is least signification digit. For signedness used two's complement method.

Nested types
digit_type DigitT
double_digit_type twice as wide as digit_type (i.e. 16 32)
reference digit_type&
const_reference const digit_type&
pointer digit_type*
const_pointer const digit_type*
iterator digit_type*
const_iterator const digit_type*
Data members
static constexpr size_t bit_width Bits
static constexpr size_t digit_width bit width of digit_type
static constexpr size_t digit_count bit_width / digit_width
static constexpr bool is_signed Signed
Static member functions
max returns the maximum value
min returns the minimum value
Creation/destruction functions
(constructor) construct a basic_integer
(destructor) destroy the basic_integer
operator= assigns values to the basic_integer
Member functions
sign_bit returns the most signification bit
sign returns the sign of integer
width returns the bit width of current value
clear sets all digits equal to zero
bit get/set bit by index
hex get/set hex digit by index
split divide the integer into two parts
merge connects a integer of two parts
expand returns bigger width integer with zero filling new digits
add_with_carry addition with an input and output carry bit
data returns a pointer to first digit
compare compares two integers
swap swaps two integers
Iterators
begin
cbegin
returns an iterator to the beginning
end
cend
returns an iterator to the end
Conversion operators
operator bool converts to bool
operator digit_type converts to digit_type
operator double_digit_type converts to double_digit_type
operator basic_integer<...> converts to another version of basic_integer
Operators
operator[] get/set digit value by index
operator+
operator-
operator~
integer unary operators
operator++
operator--
increment/decrement of integer
operator+=
operator-=
operator*=
operator/=
operator%=
operator&=
operator|=
operator^=
operator<<=
operator>>=
compound assignment binary operators
operator+
operator-
operator*
operator/
operator%
operator&
operator|
operator^
operator<<
operator>>
integer binary operators
operator==
operator!= until C++20
operator< until C++20
operator<= until C++20
operator> until C++20
operator>= until C++20
operator<=> since C++20
integer comparison operators
Example
#include "fwnbi.hpp"
#include <iostream>

using namespace fwnbi::literals;

fwnbi::uint256_t factorial(int x) {
    if (x < 0 || x > 57)
        return fwnbi::uint256_t::max();
    fwnbi::uint256_t::digit_type n = x + 1;
    fwnbi::uint256_t out = 1_ull256;
    while (n --> 1) out *= n;
    return out;
}

int main() {
    std::cout << "50! = " << factorial(50) << std::endl;
}
Output
50! = 30414093201713378043612608166064768844377641568960512000000000000