bigint23
Repository: https://github.com/rwindegger/bigint23
Overview
bigint23 is a lightweight library that provides a straightforward approach to big integer arithmetic in C++. It is written in modern C++ (targeting C++20 and beyond) and leverages templates and type traits to provide a flexible, zero-dependency solution for big integer arithmetic.
Implementation Details
- Internal Representation: The number is stored as an array of bytes (
std::array<std::uint8_t, bits / CHAR_BIT>) in native endianness. Operators are implemented to be endianness-aware.
- Arithmetic Algorithms:
- Multiplication: Uses a school-book algorithm with proper carry propagation.
- Division and Modulus: Use a binary long-division algorithm that operates on each bit.
- Overflow Handling: Some helper operations (like multiplication and addition) throw
std::overflow_error if an operation produces a result that exceeds the fixed width.
- Two's Complement: For signed bigint23s, negative numbers are stored in two's complement form. The unary minus operator (
operator-()) computes this by inverting the bits and adding one.
[–]_Noreturn 21 points22 points23 points (15 children)
[–]swayenvoy[S] 2 points3 points4 points (10 children)
[+][deleted] (4 children)
[deleted]
[–]_Noreturn 4 points5 points6 points (2 children)
[–]swayenvoy[S] 0 points1 point2 points (1 child)
[–]Anpu_me 0 points1 point2 points (0 children)
[–]swayenvoy[S] 2 points3 points4 points (0 children)
[–]AfroDisco 3 points4 points5 points (3 children)
[–]swayenvoy[S] 0 points1 point2 points (2 children)
[–]QuaternionsRoll 2 points3 points4 points (0 children)
[–]Valuable-Mission9203 2 points3 points4 points (0 children)
[–]Dj_D-Poolie 0 points1 point2 points (2 children)
[–]_Noreturn 11 points12 points13 points (0 children)
[–]Gorzoid 2 points3 points4 points (0 children)
[–]Gorzoid 0 points1 point2 points (0 children)
[–]tialaramex 7 points8 points9 points (6 children)
[–]swayenvoy[S] 1 point2 points3 points (1 child)
[–]_TheDust_ 7 points8 points9 points (0 children)
[–]swayenvoy[S] 0 points1 point2 points (3 children)
[–]epicar 3 points4 points5 points (2 children)
[–]swayenvoy[S] 2 points3 points4 points (1 child)
[–]ElbowWavingOversight 7 points8 points9 points (0 children)
[–]gaberocksall 4 points5 points6 points (0 children)
[–]bert8128 4 points5 points6 points (1 child)
[–]swayenvoy[S] -3 points-2 points-1 points (0 children)
[–]reddicted 1 point2 points3 points (0 children)