BigInt is a single-header C++ library designed for arbitrary-precision integer arithmetic. It provides a lightweight, efficient, and easy-to-use interface for handling integers that exceed the limits of built-in types like long long.
- Arbitrary Precision: Limited only by available system memory.
- Single-Header: Easy integration; just drop
bigint.hinto your project. - Modern C++: Support for C++11 and newer (C++20 compatible).
- Rich Operator Support: Full set of arithmetic, comparison, and increment/decrement operators.
- Advanced Math: Built-in support for GCD, LCM, Factorials, Power, Square Root, and Logarithms.
- Hexadecimal Support: Initialize big integers directly from hex strings.
- STL Integration: Includes a
std::hashspecialization for use inunordered_mapandunordered_set. - Random Generation: Static methods to generate cryptographically-seeded random big integers.
A C++11 (or higher) compatible compiler. The library is explicitly tested against GCC, Clang, and MSVC using C++11, 14, 17, 20, and 23.
Simply include the header file in your project.
- Download
bigint.h. - Include it in your source:
#include "bigint.h"
Note: All functionality resides within the BigInt namespace.
#include <iostream>
#include "bigint.h"
using BigInt::bigint;
int main() {
// Initialization from strings (decimal or hex)
bigint a("123456789012345678901234567890");
bigint b("0xDEADC0DEFE777");
// Initialization from primitive types
bigint small = 10;
bigint negative = -500;
// Basic Arithmetic
bigint sum = a + b;
bigint prod = a * b;
// Advanced Math
bigint p = bigint::pow(2, 1024);
bigint s = bigint::sqrt(a);
std::cout << "2^1024 = " << p << std::endl;
// STL Integration
if (a > b) {
std::cout << "a is larger" << std::endl;
}
return 0;
}The library uses the class BigInt::bigint.
bigint(): Defaults to 0.bigint(const std::string&): From decimal or hex (starts with0x) string.bigint(long long),bigint(int),bigint(double): From numeric types.bigint(const char*): From C-style strings.
- Arithmetic:
+,-,*,/,%,+=,-=,*=,/=,%= - Comparison:
==,!=,<,<=,>,>= - Increment/Decrement: Prefix and postfix
++,-- - Unary:
-(negation) - I/O:
<<(ostream) - Conversion:
explicit operator bool(),explicit operator std::string(),explicit operator int(),explicit operator long long()
bigint::pow(base, exp): Power function.bigint::sqrt(n): Integer square root.bigint::abs(n): Absolute value.bigint::gcd(a, b),bigint::lcm(a, b): Greatest Common Divisor and Least Common Multiple.bigint::factorial(n): Factorial of n.bigint::log2(n),bigint::log10(n),bigint::logwithbase(n, base): Logarithmic functions.bigint::random(digits): Generates a random positive bigint with the specified number of digits.bigint::is_prime(n): Basic primality test.bigint::is_even(n),bigint::is_negative(n): Property checks.
bigint::sum_of_digits(n): Returns the sum of all digits.std::hash<BigInt::bigint>: Specialization for hashing.
The project uses GoogleTest for verification.
git clone https://github.com/SamHerts/BigInt.git
cd BigInt/tests
cmake -B build
cmake --build build
./build/BigInt_gtestContributions are welcome!
- Fork the repository.
- Create your feature branch (
git checkout -b feature/NewFeature). - Commit your changes.
- Push to the branch.
- Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.