Skip to content

An arbitrary precision unsigned integer type for C++

License

Notifications You must be signed in to change notification settings

Kronuz/uinteger_t

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

uinteger_t License GitHub Stars GitHub Forks GitHub Watchers Tweet

Build Status

An arbitrary precision unsigned integer type for C++

This is a header-only implementation of an arbitrary precision unsigned integer type in modern C++. It's meant to be used like a standard unsigned integer, except operations can use really big numbers and can take multiple base strings as inputs.

Example

// example.cc
// g++ -std=c++14 -o example example.cc

#include <iostream>
#include "uinteger_t.hh"

int main() {
    uinteger_t a = 3735879680;
    uinteger_t b("beee", 16);  // string input (no base prefixes: '0b' or '0x')
    uinteger_t num = a + b + 1;

    std::cout << num             << std::endl;  // print directly
    // => 3735928559

    std::cout << num.str(2)     << std::endl;  // print as a string on a specific base
    // => 11011110101011011011111011101111

    std::cout << std::hex << num << std::endl;  // print using std::oct, std::dec, and std::hex
    // => deadbeef

    return 0;
}

Compilation

  • g++ and clang++ are supported.
  • C++14 is required.

Internals

Data is stored in a std::vector<uint64_t> in little-endian form, so _value[0] is the least significant digit. Operations are optimized for fast performance:

  • Addition and subtraction use regular (optimized) 64-bit operations with carry/borrow.

  • Shifts try to grow vector in the most efficient way, using a two direction growth factor of 1.5.

  • Multiplication uses long multiplication for numbers < 1024 bits and uses Karatsuba (and lopsided Karatsuba) for bigger numbers with a no-copying approach.

  • Division and modulus use long division from Knuth's Algorithm D.

Author

German Mendez Bravo (Kronuz)

Follow on GitHub Follow on Twitter

Based upon Jason Lee's uint128_t module and modified by Kronuz to support an arbitrary number of bits.

License

MIT License. See LICENSE for details.

Copyright (c) 2017 German Mendez Bravo (Kronuz) @ german dot mb at gmail.com

Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com

Releases

No releases published

Packages

No packages published

Languages

  • C++ 98.9%
  • Makefile 1.1%