A drop-in "bigint" C++ class
Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com
Please see LICENSE file for license.
With much help from:
Although this class can theoretically hold an infinitely large value,
the actual limit of how large the integers can be isstd::deque <Z>().max_size() * sizeof(Z) * 8 bits
, which
should be more than enough for any purpose.
#include <iostream>
#include "integer.h"
int main(){
integer a = -1234; // standard integer input
integer b( "5678", 16); // string input (no base prefixes: '0b' or '0x')
integer c( "-9abC", 16); // hex input cases can be mixed
integer d("Hello!", 256); // ASCII strings can be used too!
integer sum = a + b + c + d + 1; // 79600447923724 (decimal)
std::cout << sum << std::endl; // print directly
std::cout << sum.str(16) << std::endl; // print with a specific base
std::cout << std::hex << sum << std::endl; // print using std::oct, std::dec, and std::hex
return 0;
}
c++ <some arguments> integer.cpp <other arguments>
Data is stored in big-endian, so value[0] is the most
significant digit, and value[value.size() - 1] is the
least significant digit.
By default, the container holding the value is std::deque <uint8_t>
.
Changing the internal representation to a std::string
makes integer run slower than using a std::deque <uint8_t>
Negative values are stored as their positive value,
with a bool that says the value is negative.
Arithmetic (+, -, *, /, %) and Comparison (>, >=, <, <=, ==, !=) operators
operate on the absolute values of the operands from within private functions.
Signs are handled in the operators themselves.
Bitwise operations on negative values will use the
two’s complement version of the number.
Conversions for strings of values in bases [2, 16] and 256 are provided.
If others are required, add the conversions tointeger::integer(Iterator, const Iterator&, const uint16_t &)
andstd::string integer::str(const uint16_t &, const unsigned int &) const
The constructor using iterators allows for creating values
from any integral base larger than 1.
Hexadecimal output strings use lowercase characters.
Base256 strings are assumed to be positive when read into
integer. Use operator-() to negate the value.