30 #ifndef BIG_INTEGER_BASE_IMPLEMENTATION_GUARD
31 #define BIG_INTEGER_BASE_IMPLEMENTATION_GUARD
40 template <
typename T_Impl>
42 T_Impl::Initialize(*
this);
48 template <
typename T_Impl>
50 T_Impl::Initialize(*
this, input);
56 template <
typename T_Impl>
58 T_Impl::Initialize(*
this, input);
64 template <
typename T_Impl>
66 T_Impl::Initialize(*
this, input);
72 template <
typename T_Impl>
74 T_Impl::Initialize(*
this, static_cast<long>(input));
80 template <
typename T_Impl>
82 T_Impl::Initialize(*
this, static_cast<unsigned long>(input));
90 template <
typename T_Impl>
92 T_Impl::Initialize(*
this, input, scaling, truncate);
100 template <
typename T_Impl>
102 T_Impl::Initialize(*
this, input, numberOfDigits, truncate);
111 template <
typename T_Impl>
113 T_Impl::Initialize(*
this, input, base);
121 template <
typename T_Impl>
123 T_Impl::Destroy(*
this);
134 template <
typename T_Impl>
136 T_Impl::Set(*
this, input);
144 template <
typename T_Impl>
146 T_Impl::Set(*
this, input);
154 template <
typename T_Impl>
156 T_Impl::Set(*
this, input);
164 template <
typename T_Impl>
166 T_Impl::Set(*
this, static_cast<long>(input));
174 template <
typename T_Impl>
176 T_Impl::Set(*
this, static_cast<unsigned long>(input));
183 template <
typename T_Impl>
192 template <
typename T_Impl>
196 T_Impl::InvertSign(output, *
this);
204 template <
typename T_Impl>
206 T_Impl::Add(*
this, *
this, 1L);
213 template <
typename T_Impl>
217 T_Impl::Add(*
this, *
this, 1L);
225 template <
typename T_Impl>
227 T_Impl::Subtract(*
this, *
this, 1L);
234 template <
typename T_Impl>
238 T_Impl::Subtract(*
this, *
this, 1L);
247 template <
typename T_Impl>
251 T_Impl::Add(output, *
this, input);
260 template <
typename T_Impl>
264 T_Impl::Add(output, *
this, input);
273 template <
typename T_Impl>
277 T_Impl::Add(output, *
this, input);
286 template <
typename T_Impl>
290 T_Impl::Add(output, *
this, static_cast<long>(input));
299 template <
typename T_Impl>
303 T_Impl::Add(output, *
this, static_cast<unsigned long>(input));
312 template <
typename T_Impl>
314 T_Impl::Add(*
this, *
this, input);
322 template <
typename T_Impl>
324 T_Impl::Add(*
this, *
this, input);
332 template <
typename T_Impl>
334 T_Impl::Add(*
this, *
this, input);
342 template <
typename T_Impl>
344 T_Impl::Add(*
this, *
this, static_cast<long>(input));
352 template <
typename T_Impl>
354 T_Impl::Add(*
this, *
this, static_cast<unsigned long>(input));
362 template <
typename T_Impl>
366 T_Impl::Subtract(output, *
this, input);
375 template <
typename T_Impl>
379 T_Impl::Subtract(output, *
this, input);
388 template <
typename T_Impl>
392 T_Impl::Subtract(output, *
this, input);
401 template <
typename T_Impl>
405 T_Impl::Subtract(output, *
this, static_cast<long>(input));
414 template <
typename T_Impl>
418 T_Impl::Subtract(output, *
this, static_cast<unsigned long>(input));
427 template <
typename T_Impl>
429 T_Impl::Subtract(*
this, *
this, input);
437 template <
typename T_Impl>
439 T_Impl::Subtract(*
this, *
this, input);
447 template <
typename T_Impl>
449 T_Impl::Subtract(*
this, *
this, input);
457 template <
typename T_Impl>
459 T_Impl::Subtract(*
this, *
this, static_cast<long>(input));
467 template <
typename T_Impl>
469 T_Impl::Subtract(*
this, *
this, static_cast<unsigned long>(input));
477 template <
typename T_Impl>
481 T_Impl::Multiply(output, *
this, input);
490 template <
typename T_Impl>
494 T_Impl::Multiply(output, *
this, input);
503 template <
typename T_Impl>
507 T_Impl::Multiply(output, *
this, input);
516 template <
typename T_Impl>
520 T_Impl::Multiply(output, *
this, static_cast<long>(input));
529 template <
typename T_Impl>
533 T_Impl::Multiply(output, *
this, static_cast<unsigned long>(input));
542 template <
typename T_Impl>
544 T_Impl::Multiply(*
this, *
this, input);
552 template <
typename T_Impl>
554 T_Impl::Multiply(*
this, *
this, input);
562 template <
typename T_Impl>
564 T_Impl::Multiply(*
this, *
this, input);
572 template <
typename T_Impl>
574 T_Impl::Multiply(*
this, *
this, static_cast<long>(input));
582 template <
typename T_Impl>
584 T_Impl::Multiply(*
this, *
this, static_cast<unsigned long>(input));
592 template <
typename T_Impl>
596 T_Impl::Divide(output, *
this, input);
605 template <
typename T_Impl>
609 T_Impl::Divide(output, *
this, input);
618 template <
typename T_Impl>
622 T_Impl::Divide(output, *
this, input);
631 template <
typename T_Impl>
635 T_Impl::Divide(output, *
this, static_cast<long>(input));
644 template <
typename T_Impl>
648 T_Impl::Divide(output, *
this, static_cast<unsigned long>(input));
657 template <
typename T_Impl>
659 T_Impl::Divide(*
this, *
this, input);
667 template <
typename T_Impl>
669 T_Impl::Divide(*
this, *
this, input);
677 template <
typename T_Impl>
679 T_Impl::Divide(*
this, *
this, input);
687 template <
typename T_Impl>
689 T_Impl::Divide(*
this, *
this, static_cast<long>(input));
697 template <
typename T_Impl>
699 T_Impl::Divide(*
this, *
this, static_cast<unsigned long>(input));
707 template <
typename T_Impl>
711 T_Impl::Modulo(output, *
this, input);
720 template <
typename T_Impl>
724 T_Impl::Modulo(output, *
this, input);
733 template <
typename T_Impl>
737 T_Impl::Modulo(output, *
this, input);
746 template <
typename T_Impl>
750 T_Impl::Modulo(output, *
this, static_cast<long>(input));
759 template <
typename T_Impl>
763 T_Impl::Modulo(output, *
this, static_cast<unsigned long>(input));
772 template <
typename T_Impl>
774 T_Impl::Modulo(*
this, *
this, input);
782 template <
typename T_Impl>
784 T_Impl::Modulo(*
this, *
this, input);
792 template <
typename T_Impl>
794 T_Impl::Modulo(*
this, *
this, input);
802 template <
typename T_Impl>
804 T_Impl::Modulo(*
this, *
this, static_cast<long>(input));
812 template <
typename T_Impl>
814 T_Impl::Modulo(*
this, *
this, static_cast<unsigned long>(input));
822 template <
typename T_Impl>
824 return T_Impl::Compare(*
this, input) == 0 ?
true :
false;
831 template <
typename T_Impl>
833 return T_Impl::Compare(*
this, input) == 0 ?
true :
false;
840 template <
typename T_Impl>
842 return T_Impl::Compare(*
this, input) == 0 ?
true :
false;
849 template <
typename T_Impl>
851 return T_Impl::Compare(*
this, static_cast<long>(input)) == 0 ?
true :
false;
858 template <
typename T_Impl>
860 return T_Impl::Compare(*
this, static_cast<unsigned long>(input)) == 0 ?
true :
false;
867 template <
typename T_Impl>
869 return T_Impl::Compare(*
this, input) != 0 ?
true :
false;
876 template <
typename T_Impl>
878 return T_Impl::Compare(*
this, input) != 0 ?
true :
false;
885 template <
typename T_Impl>
887 return T_Impl::Compare(*
this, input) != 0 ?
true :
false;
894 template <
typename T_Impl>
896 return T_Impl::Compare(*
this, static_cast<long>(input)) != 0 ?
true :
false;
903 template <
typename T_Impl>
905 return T_Impl::Compare(*
this, static_cast<unsigned long>(input)) != 0 ?
true :
false;
912 template <
typename T_Impl>
914 return T_Impl::Compare(*
this, input) < 0 ?
true :
false;
921 template <
typename T_Impl>
923 return T_Impl::Compare(*
this, input) < 0 ?
true :
false;
930 template <
typename T_Impl>
932 return T_Impl::Compare(*
this, input) < 0 ?
true :
false;
939 template <
typename T_Impl>
941 return T_Impl::Compare(*
this, static_cast<long>(input)) < 0 ?
true :
false;
948 template <
typename T_Impl>
950 return T_Impl::Compare(*
this, static_cast<unsigned long>(input)) < 0 ?
true :
false;
957 template <
typename T_Impl>
959 return T_Impl::Compare(*
this, input) <= 0 ?
true :
false;
966 template <
typename T_Impl>
968 return T_Impl::Compare(*
this, input) <= 0 ?
true :
false;
975 template <
typename T_Impl>
977 return T_Impl::Compare(*
this, input) <= 0 ?
true :
false;
984 template <
typename T_Impl>
986 return T_Impl::Compare(*
this, static_cast<long>(input)) <= 0 ?
true :
false;
993 template <
typename T_Impl>
995 return T_Impl::Compare(*
this, static_cast<unsigned long>(input)) <= 0 ?
true :
false;
1002 template <
typename T_Impl>
1004 return T_Impl::Compare(*
this, input) >= 0 ?
true :
false;
1011 template <
typename T_Impl>
1013 return T_Impl::Compare(*
this, input) >= 0 ?
true :
false;
1020 template <
typename T_Impl>
1022 return T_Impl::Compare(*
this, input) >= 0 ?
true :
false;
1029 template <
typename T_Impl>
1031 return T_Impl::Compare(*
this, static_cast<long>(input)) >= 0 ?
true :
false;
1038 template <
typename T_Impl>
1040 return T_Impl::Compare(*
this, static_cast<unsigned long>(input)) >= 0 ?
true :
false;
1047 template <
typename T_Impl>
1049 return T_Impl::Compare(*
this, input) > 0 ?
true :
false;
1056 template <
typename T_Impl>
1058 return T_Impl::Compare(*
this, input) > 0 ?
true :
false;
1065 template <
typename T_Impl>
1067 return T_Impl::Compare(*
this, input) > 0 ?
true :
false;
1074 template <
typename T_Impl>
1076 return T_Impl::Compare(*
this, static_cast<long>(input)) > 0 ?
true :
false;
1083 template <
typename T_Impl>
1085 return T_Impl::Compare(*
this, static_cast<unsigned long>(input)) > 0 ?
true :
false;
1092 template <
typename T_Impl>
1096 T_Impl::RightShift(output, *
this, input);
1105 template <
typename T_Impl>
1107 T_Impl::RightShift(*
this, *
this, input);
1115 template <
typename T_Impl>
1119 T_Impl::LeftShift(output, *
this, input);
1128 template <
typename T_Impl>
1130 T_Impl::LeftShift(*
this, *
this, input);
1137 template <
typename T_Impl>
1141 T_Impl::InvertBits(output, *
this);
1150 template <
typename T_Impl>
1154 T_Impl::BitwiseAnd(output, *
this, input);
1163 template <
typename T_Impl>
1165 T_Impl::BitwiseAnd(*
this, *
this, input);
1173 template <
typename T_Impl>
1177 T_Impl::BitwiseOr(output, *
this, input);
1186 template <
typename T_Impl>
1188 T_Impl::BitwiseOr(*
this, *
this, input);
1196 template <
typename T_Impl>
1200 T_Impl::BitwiseXor(output, *
this, input);
1209 template <
typename T_Impl>
1211 T_Impl::BitwiseXor(*
this, *
this, input);
1223 template <
typename T_Impl>
1227 T_Impl::GetNextPrime(output, *
this);
1235 template <
typename T_Impl>
1237 return T_Impl::IsPrime(*
this);
1244 template <
typename T_Impl>
1246 T_Impl::SetBit(*
this, index);
1254 template <
typename T_Impl>
1256 return T_Impl::GetBit(*
this, index);
1265 template <
typename T_Impl>
1267 return T_Impl::GetSize(*
this, base);
1273 template <
typename T_Impl>
1275 T_Impl::Abs(*
this, *
this);
1282 template <
typename T_Impl>
1286 T_Impl::Abs(output, *
this);
1295 template <
typename T_Impl>
1297 T_Impl::Pow(*
this, *
this, power);
1305 template <
typename T_Impl>
1307 T_Impl::Pow(*
this, *
this, power);
1315 template <
typename T_Impl>
1317 T_Impl::Pow(*
this, *
this, power);
1325 template <
typename T_Impl>
1327 T_Impl::Pow(*
this, *
this, static_cast<long>(power));
1335 template <
typename T_Impl>
1337 T_Impl::Pow(*
this, *
this, static_cast<unsigned long>(power));
1345 template <
typename T_Impl>
1349 T_Impl::Pow(output, *
this, power);
1358 template <
typename T_Impl>
1362 T_Impl::Pow(output, *
this, power);
1371 template <
typename T_Impl>
1375 T_Impl::Pow(output, *
this, power);
1384 template <
typename T_Impl>
1388 T_Impl::Pow(output, *
this, static_cast<long>(power));
1397 template <
typename T_Impl>
1401 T_Impl::Pow(output, *
this, static_cast<unsigned long>(power));
1411 template <
typename T_Impl>
1413 T_Impl::PowModN(*
this, *
this, power, n);
1422 template <
typename T_Impl>
1424 T_Impl::PowModN(*
this, *
this, power, n);
1433 template <
typename T_Impl>
1435 T_Impl::PowModN(*
this, *
this, power, n);
1444 template <
typename T_Impl>
1446 T_Impl::PowModN(*
this, *
this, static_cast<long>(power), n);
1455 template <
typename T_Impl>
1457 T_Impl::PowModN(*
this, *
this, static_cast<unsigned long>(power), n);
1466 template <
typename T_Impl>
1470 T_Impl::PowModN(output, *
this, power, n);
1480 template <
typename T_Impl>
1484 T_Impl::PowModN(output, *
this, power, n);
1494 template <
typename T_Impl>
1498 T_Impl::PowModN(output, *
this, power, n);
1508 template <
typename T_Impl>
1512 T_Impl::PowModN(output, *
this, static_cast<long>(power), n);
1522 template <
typename T_Impl>
1526 T_Impl::PowModN(output, *
this, static_cast<unsigned long>(power), n);
1535 template <
typename T_Impl>
1537 T_Impl::InvertModN(*
this, *
this, n);
1545 template <
typename T_Impl>
1549 T_Impl::InvertModN(output, *
this, n);
1562 template <
typename T_Impl>
1564 T_Impl::Swap(lhs, rhs);
1572 template <
typename T_Impl>
1576 T_Impl::Gcd(output, lhs, rhs);
1586 template <
typename T_Impl>
1590 T_Impl::Lcm(output, lhs, rhs);
1603 template <
typename T_Impl>
1605 return T_Impl::ToString(*
this, base);
1611 template <
typename T_Impl>
1613 return T_Impl::ToUnsignedLong(*
this);
1625 template <
typename T_Impl>
1635 template <
typename T_Impl>
1645 template <
typename T_Impl>
1655 template <
typename T_Impl>
1665 template <
typename T_Impl>
1679 template <
typename T_Impl>
1693 template <
typename T_Impl>
1707 template <
typename T_Impl>
1721 template <
typename T_Impl>
1731 template <
typename T_Impl>
1741 template <
typename T_Impl>
1751 template <
typename T_Impl>
1761 template <
typename T_Impl>
1775 template <
typename T_Impl>
1789 template <
typename T_Impl>
1803 template <
typename T_Impl>
1817 template <
typename T_Impl>
1831 template <
typename T_Impl>
1845 template <
typename T_Impl>
1859 template <
typename T_Impl>
1873 template <
typename T_Impl>
1883 template <
typename T_Impl>
1893 template <
typename T_Impl>
1903 template <
typename T_Impl>
1913 template <
typename T_Impl>
1923 template <
typename T_Impl>
1933 template <
typename T_Impl>
1943 template <
typename T_Impl>
1953 template <
typename T_Impl>
1954 bool operator< (const long lhs, const BigIntegerBase<T_Impl> &rhs) {
1963 template <
typename T_Impl>
1964 bool operator< (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs) {
1973 template <
typename T_Impl>
1974 bool operator< (const int lhs, const BigIntegerBase<T_Impl> &rhs) {
1983 template <
typename T_Impl>
1984 bool operator< (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs) {
1993 template <
typename T_Impl>
1994 bool operator<= (const long lhs, const BigIntegerBase<T_Impl> &rhs) {
2003 template <
typename T_Impl>
2004 bool operator<= (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs) {
2013 template <
typename T_Impl>
2014 bool operator<= (const int lhs, const BigIntegerBase<T_Impl> &rhs) {
2023 template <
typename T_Impl>
2024 bool operator<= (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs) {
2033 template <
typename T_Impl>
2043 template <
typename T_Impl>
2053 template <
typename T_Impl>
2063 template <
typename T_Impl>
2073 template <
typename T_Impl>
2083 template <
typename T_Impl>
2093 template <
typename T_Impl>
2103 template <
typename T_Impl>
2113 #endif//BIG_INTEGER_BASE_IMPLEMENTATION_GUARD
BigIntegerBase< T_Impl > & operator++()
Prefix increment unary operator.
BigIntegerBase< T_Impl > & operator--()
Prefix decrement unary operator.
bool operator>=(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase greater than or equal to binary operator.
BigIntegerBase< T_Impl > & SetBit(const size_t index)
Sets a bit in the current instance at the specified index.
bool operator<(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase less than binary operator.
BigIntegerBase< T_Impl > operator*(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long multiplication binary operator
int GetBit(const size_t index) const
Returns the bit specified by index.
BigIntegerBase< T_Impl > GetNextPrime() const
Computes the first prime greater than the current instance.
bool operator>(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase greater than binary operator.
BigIntegerBase< T_Impl > operator~() const
One's complement unary operator.
std::string ToString(const unsigned int base=2) const
Convert to std::string in the specified base.
BigIntegerBase< T_Impl > operator>>(const unsigned long input) const
Bitwise right shift binary operator.
BigIntegerBase< T_Impl > GetPowModN(const BigIntegerBase< T_Impl > &power, const BigIntegerBase< T_Impl > &n) const
Computes the integer raised to the specified BigIntegerBase power modulo n.
BigIntegerBase< T_Impl > & operator-=(const BigIntegerBase< T_Impl > &input)
BigIntegerBase subtraction & assignment binary operator.
BigIntegerBase< T_Impl > & operator|=(const BigIntegerBase< T_Impl > &input)
Bitwise inclusive OR & assignment binary operator.
bool operator==(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long equality binary operator
unsigned long ToUnsignedLong() const
Convert to unsigned long.
BigIntegerBase< T_Impl > & operator<<=(const unsigned long input)
Bitwise left shift & assignment binary operator.
BigIntegerBase< T_Impl > & operator&=(const BigIntegerBase< T_Impl > &input)
Bitwise AND & assignment binary operator.
BigIntegerBase< T_Impl > & operator>>=(const unsigned long input)
Bitwise right shift & assignment binary operator.
BigIntegerBase< T_Impl > operator&(const BigIntegerBase< T_Impl > &input) const
Bitwise AND binary operator.
bool operator>=(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long greater than or equal binary operator
BigIntegerBase< T_Impl > operator<<(const unsigned long input) const
Bitwise left shift binary operator.
BigIntegerBase< T_Impl > & Abs()
Sets the current instance to its absolute value.
BigIntegerBase< T_Impl > & operator/=(const BigIntegerBase< T_Impl > &input)
BigIntegerBase division & assignment binary operator.
bool IsPrime() const
Tests if the current instance is a prime number.
BigIntegerBase< T_Impl > operator/(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase division binary operator.
size_t GetSize(const unsigned int base=2) const
Gets the length of the integer in the specified base.
BigIntegerBase< T_Impl > operator%(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long modulus binary operator
BigIntegerBase< T_Impl > operator-() const
Unary negation operator.
BigIntegerBase()
Default constructor.
BigIntegerBase< T_Impl > operator+() const
Unary plus operator.
static BigIntegerBase< T_Impl > Gcd(const BigIntegerBase< T_Impl > &lhs, const BigIntegerBase< T_Impl > &rhs)
Computes the greatest common divisor of lhs and rhs.
BigIntegerBase< T_Impl > & Pow(const BigIntegerBase< T_Impl > &power)
Raises the current instance to the specified BigIntegerBase power.
BigIntegerBase< T_Impl > operator|(const BigIntegerBase< T_Impl > &input) const
Bitwise inclusive OR binary operator.
BigIntegerBase< T_Impl > operator^(const BigIntegerBase< T_Impl > &input) const
Bitwise exclusive OR binary operator.
bool operator==(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase equality binary operator.
BigIntegerBase< T_Impl > GetPow(const BigIntegerBase< T_Impl > &power) const
Computes the integer raised to the specified BigIntegerBase power.
BigIntegerBase< T_Impl > & operator^=(const BigIntegerBase< T_Impl > &input)
Bitwise exclusive OR & assignment binary operator.
bool operator!=(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long inequality binary operator
BigIntegerBase< T_Impl > & PowModN(const BigIntegerBase< T_Impl > &power, const BigIntegerBase< T_Impl > &n)
Raises the current instance to the specified BigIntegerBase power modulo n.
BigIntegerBase< T_Impl > & operator*=(const BigIntegerBase< T_Impl > &input)
BigIntegerBase multiplication & assignment binary operator.
BigIntegerBase< T_Impl > & operator+=(const BigIntegerBase< T_Impl > &input)
BigIntegerBase addition & assignment binary operator.
BigIntegerBase< T_Impl > & operator=(const BigIntegerBase< T_Impl > &input)
BigIntegerBase assignment operator.
BigIntegerBase< T_Impl > GetAbs() const
Computes the absolute value of the current instance.
Template class which adds syntactic sugar to big integer operations.
BigIntegerBase< T_Impl > operator+(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long addition binary operator
bool operator<=(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase less than or equal to binary operator.
static void Swap(BigIntegerBase< T_Impl > &lhs, BigIntegerBase< T_Impl > &rhs)
Swaps lhs with rhs efficiently.
BigIntegerBase< T_Impl > operator*(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase multiplication binary operator.
BigIntegerBase< T_Impl > operator-(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long subtraction binary operator
~BigIntegerBase()
Destructor.
BigIntegerBase< T_Impl > GetInverseModN(const BigIntegerBase< T_Impl > &n) const
Computes the inverse modulo n.
BigIntegerBase< T_Impl > & operator%=(const BigIntegerBase< T_Impl > &input)
BigIntegerBase modulus & assignment binary operator.
bool operator!=(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase inequality binary operator.
BigIntegerBase< T_Impl > & InvertModN(const BigIntegerBase< T_Impl > &n)
Inverts the current instance modulo n.
static BigIntegerBase< T_Impl > Lcm(const BigIntegerBase< T_Impl > &lhs, const BigIntegerBase< T_Impl > &rhs)
Computes the least common multiple of lhs and rhs.
BigIntegerBase< T_Impl > operator/(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long division binary operator
bool operator>(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long greater than binary operator
BigIntegerBase< T_Impl > operator%(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase modulus binary operator.