TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
uint256_t Class Reference

class for 256-bit unsigned integer More...

#include <uint256_t.hpp>

Collaboration diagram for uint256_t:
[legend]

Public Member Functions

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
 uint256_t (T low)
 Parameterized constructor.
 uint256_t (const std::string &str)
 Parameterized constructor.
 uint256_t (const uint256_t &num)=default
 Copy constructor.
 uint256_t (uint256_t &&num) noexcept
 Move constructor.
 uint256_t (uint128_t high, uint128_t low)
 Parameterized constructor.
 uint256_t (const uint64_t high, const uint64_t low)
 Parameterized constructor.
 ~uint256_t ()=default
 Destructor for uint256_t.
uint32_t _lez ()
 Leading zeroes in binary.
uint32_t _trz ()
 Trailing zeroes in binary.
 operator bool () const
 casting operator to boolean value
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
 operator T () const
 casting operator to any integer value
 operator uint128_t () const
 casting operator to uint128_t
uint128_t lower () const
 returns lower 128-bit integer part
uint128_t upper () const
 returns upper 128-bit integer part
uint256_t & operator= (const uint256_t &p)=default
 operator = for uint256_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & operator= (const T &p)
 operator = for other types
uint256_t & operator= (const std::string &p)
 operator = for type string
uint256_t & operator= (uint256_t &&p)=default
 Move assignment operator.
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t operator+ (const T &p)
 operator + for uint256_t and other integer types.
uint256_t operator+ (const uint256_t &p)
 operator + for uint256_t and other integer types.
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & operator+= (const T &p)
 operator += for uint256_t and other integer types.
uint256_t & operator+= (const uint256_t &p)
 operator += for uint256_t
uint256_t & operator++ ()
 pre-increment operator
uint256_t operator++ (int)
 post-increment operator
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t operator- (const T &p)
 operator - for uint256_t and other integer types.
uint256_t operator- (const uint256_t &p)
 operator - for uint256_t
uint256_t operator- ()
 operator - using twos complement
uint256_t & operator-- ()
 operator – (pre-decrement)
uint256_t operator-- (int)
 operator – (post-decrement)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t operator-= (const T p)
 operator -= for uint256_t and other integer types.
uint256_t & operator-= (const uint256_t &p)
 operator -= for uint256_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t operator* (const T &p)
 operator * for uint256_t and other integer types.
uint256_t operator* (const uint256_t &p)
 operator * for uint256_t and other integer types.
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & operator*= (const T &p)
 operator *= for uint256_t and other integer types.
uint256_t & operator*= (const uint256_t &p)
 operator *= for uint256_t and other integer types.
std::pair< uint256_t, uint256_t > divide (const uint256_t &p)
 divide function for uint256_t and other integer types.
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t operator/ (const T &p)
 operator / for uint256_t and other integer types.
uint256_t operator/ (const uint256_t &p)
 operator / for uint256_t and other integer types.
uint256_t & operator/= (const uint256_t &p)
 operator /= for uint256_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & operator/= (const T &p)
 operator /= for uint256_t and other integer types.
uint256_t operator% (const uint256_t &p)
 operator % for uint256_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t operator% (const T &p)
 operator % for uint256_t and other integer types.
uint256_t & operator%= (const uint256_t &p)
 operator %= for uint256_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & operator%= (const T &p)
 operator %= for uint256_t
bool operator< (const uint256_t &other)
 operator < for uint256_t
bool operator<= (const uint256_t &other)
 operator <= for uint256_t
bool operator> (const uint256_t &other)
 operator > for uint256_t
bool operator>= (const uint256_t &other)
 operator >= for uint256_t
bool operator== (const uint256_t &other)
 operator == for uint256_t
bool operator!= (const uint256_t &other)
 operator != for uint256_t
bool operator! ()
 operator ! for uint256_t
bool operator&& (const uint256_t &b)
 operator && for uint256_t
bool operator|| (const uint256_t &b)
 operator || for uint256_t
bool operator() ()
 operator () for uint256_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator< (const T &other)
 operator < for other types
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator<= (const T &other)
 operator <= for other types
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator> (const T &other)
 operator > for other types
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator>= (const T &other)
 operator >= for other types
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator== (const T &other)
 operator == for other types
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator!= (const T &other)
 operator != for other types
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator&& (const T &b)
 operator && for other types
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator|| (const T &b)
 operator || for other types
uint256_t operator~ ()
 operator ~ for uint256_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t operator<< (const T &p)
 operator << for uint256_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & operator<<= (const T &p)
 operator <<= for uint256_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t operator>> (const T &p)
 operator >> for uint256_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & operator>>= (const T &p)
 operator >>= for uint256_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t operator& (const T &p)
 operator & for other types (bitwise operator)
uint256_t operator& (const uint256_t &p)
 operator & for uint256_t (bitwise operator)
uint256_t & operator&= (const uint256_t &p)
 operator &= for uint256_t (bitwise operator)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & operator&= (const T p)
 operator &= for other types (bitwise operator)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t operator| (const T &p)
 operator | for other types (bitwise operator)
uint256_t operator| (const uint256_t &p)
 operator | for uint256_t (bitwise operator)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & operator|= (const T &p)
 operator |= for other types (bitwise operator)
uint256_t & operator|= (const uint256_t &p)
 operator |= for uint256_t (bitwise operator)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t operator^ (const T &p)
 operator ^ for other types (bitwise operator)
uint256_t operator^ (const uint256_t &p)
 operator ^ for uint256_t (bitwise operator)
uint256_t & operator^= (const uint256_t &p)
 operator ^= for uint256_t (bitwise operator)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & operator^= (const T &p)
 operator ^= for other types (bitwise operator)

Private Member Functions

void __get_integer_from_string (const std::string &str)
 First and second half of 256 bit number.

Private Attributes

uint128_t f {}
uint128_t s {}

Friends

std::ostream & operator<< (std::ostream &op, uint256_t p)
 operator << for printing uint256_t integer

Detailed Description

class for 256-bit unsigned integer

Definition at line 24 of file uint256_t.hpp.

Constructor & Destructor Documentation

◆ uint256_t() [1/6]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t::uint256_t ( T low)
inlineexplicit

Parameterized constructor.

Template Parameters
Ttemplate for integer types
Parameters
lowInteger denoting lower 128-bits

Definition at line 66 of file uint256_t.hpp.

66: f(0), s(low) {}

◆ uint256_t() [2/6]

uint256_t::uint256_t ( const std::string & str)
inlineexplicit

Parameterized constructor.

Parameters
strInteger string (hexadecimal starting with 0x.. or decimal)

Definition at line 72 of file uint256_t.hpp.

72 {
74 }
void __get_integer_from_string(const std::string &str)
First and second half of 256 bit number.
Definition uint256_t.hpp:34

◆ uint256_t() [3/6]

uint256_t::uint256_t ( const uint256_t & num)
default

Copy constructor.

Parameters
num256-bit unsigned integer

◆ uint256_t() [4/6]

uint256_t::uint256_t ( uint256_t && num)
inlinenoexcept

Move constructor.

Parameters
num256-bit unsigned integer

Definition at line 86 of file uint256_t.hpp.

87 : f(std::move(num.f)), s(std::move(num.s)) {}

◆ uint256_t() [5/6]

uint256_t::uint256_t ( uint128_t high,
uint128_t low )
inline

Parameterized constructor.

Parameters
highhigher part 128-bit unsigned integer
lowlower part 128-bit unsigned integer

Definition at line 94 of file uint256_t.hpp.

95 : f(std::move(high)), s(std::move(low)) {}

◆ uint256_t() [6/6]

uint256_t::uint256_t ( const uint64_t high,
const uint64_t low )
inline

Parameterized constructor.

Parameters
highhigher part 64-bit unsigned integer
lowlower part 64-bit unsigned integer

Definition at line 102 of file uint256_t.hpp.

102: f(high), s(low) {}

Member Function Documentation

◆ __get_integer_from_string()

void uint256_t::__get_integer_from_string ( const std::string & str)
inlineprivate

First and second half of 256 bit number.

Get integer from given string.

Create an integer from a given string

Parameters
strinteger string, can be hexadecimal (starting on 0x... or number)
Returns
void

Definition at line 34 of file uint256_t.hpp.

34 {
35 this->f = this->s = uint128_t(0);
36 if (str.size() > 1 && str[1] == 'x') {
37 for (auto i = 2; i < str.size(); ++i) {
38 *this *= 16LL;
39 if (str[i] >= '0' && str[i] <= '9') {
40 *this += (str[i] - '0');
41 } else if (str[i] >= 'A' && str[i] <= 'F') {
42 *this += (str[i] - 'A' + 10);
43 } else if (str[i] >= 'a' && str[i] <= 'f') {
44 *this += (str[i] - 'a' + 10);
45 }
46 }
47 } else {
48 for (auto &x : str) {
49 *this *= 10LL;
50 *this += (x - '0');
51 }
52 }
53 }

◆ _lez()

uint32_t uint256_t::_lez ( )
inline

Leading zeroes in binary.

Calculates leading zeros in 256-bit integer

Returns
Integer denoting leading zeroes

Definition at line 114 of file uint256_t.hpp.

114 {
115 if (f) {
116 return f._lez();
117 }
118 return 128 + s._lez();
119 }

◆ _trz()

uint32_t uint256_t::_trz ( )
inline

Trailing zeroes in binary.

Calculates leading zeros in 256-bit integer

Returns
Integer denoting Trailing zeroes

Definition at line 126 of file uint256_t.hpp.

126 {
127 if (s) {
128 return s._trz();
129 }
130 return 128 + f._trz();
131 }

◆ divide()

std::pair< uint256_t, uint256_t > uint256_t::divide ( const uint256_t & p)
inline

divide function for uint256_t and other integer types.

divide this value and

Parameters
p256-bit unsigned integer
Returns
pair denoting quotient and remainder.

Definition at line 412 of file uint256_t.hpp.

412 {
413 if (*this < p) { // if this is less than divisor
414 return {uint256_t(0), *this};
415 } else if (*this == p) { // if this is equal to divisor
416 return {uint256_t(1), uint256_t(0)};
417 }
418 uint256_t tmp = p, tmp2 = *this;
419 uint16_t left = tmp._lez() - _lez();
420 tmp <<= left;
421 uint256_t quotient(0);
422 uint256_t zero(0);
423 while (tmp2 >= p) {
424 uint16_t shf = tmp2._lez() - tmp._lez();
425 if (shf) {
426 tmp >>= shf;
427 quotient <<= shf;
428 left -= shf;
429 }
430 if (tmp2 < tmp) {
431 tmp >>= 1;
432 quotient <<= 1;
433 --left;
434 }
435 tmp2 -= tmp;
436 ++quotient;
437 }
438 return {quotient << left, tmp2};
439 }
uint32_t _lez()
Leading zeroes in binary.

◆ lower()

uint128_t uint256_t::lower ( ) const
inline

returns lower 128-bit integer part

Returns
returns lower 128-bit integer part

Definition at line 160 of file uint256_t.hpp.

160{ return s; }

◆ operator bool()

uint256_t::operator bool ( ) const
inlineexplicit

casting operator to boolean value

Returns
true if value of this is non-zero, else false

Definition at line 137 of file uint256_t.hpp.

137{ return f || s; }

◆ operator T()

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t::operator T ( ) const
inlineexplicit

casting operator to any integer value

Template Parameters
Tany integer type
Returns
integer value casted to mentioned type

Definition at line 146 of file uint256_t.hpp.

146 {
147 return static_cast<T>(s);
148 }

◆ operator uint128_t()

uint256_t::operator uint128_t ( ) const
inlineexplicit

casting operator to uint128_t

Returns
returns lower 128-bit integer part

Definition at line 154 of file uint256_t.hpp.

154{ return s; }

◆ operator!()

bool uint256_t::operator! ( )
inline

operator ! for uint256_t

Returns
true if this has zero value, else false

Definition at line 587 of file uint256_t.hpp.

587{ return !f && !s; }

◆ operator!=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint256_t::operator!= ( const T & other)
inline

operator != for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is not equal to other, else false

Definition at line 681 of file uint256_t.hpp.

681 {
682 return *this != uint256_t(other);
683 }

◆ operator!=() [2/2]

bool uint256_t::operator!= ( const uint256_t & other)
inline

operator != for uint256_t

Parameters
othernumber to be compared with this
Returns
true if this is not equal than other, else false

Definition at line 579 of file uint256_t.hpp.

579 {
580 return !((*this) == other);
581 }

◆ operator%() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t uint256_t::operator% ( const T & p)
inline

operator % for uint256_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
unsigned 256-bit remainder.

Definition at line 500 of file uint256_t.hpp.

500 {
501 uint256_t tmp = *this;
502 tmp %= uint256_t(p);
503 return tmp;
504 }

◆ operator%() [2/2]

uint256_t uint256_t::operator% ( const uint256_t & p)
inline

operator % for uint256_t

Parameters
p256-bit unsigned integer
Returns
unsigned 256-bit remainder.

Definition at line 490 of file uint256_t.hpp.

490{ return divide(p).second; }
std::pair< uint256_t, uint256_t > divide(const uint256_t &p)
divide function for uint256_t and other integer types.

◆ operator%=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & uint256_t::operator%= ( const T & p)
inline

operator %= for uint256_t

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
this set as unsigned 256-bit remainder.

Definition at line 524 of file uint256_t.hpp.

524 {
525 *this %= uint256_t(p);
526 return *this;
527 }

◆ operator%=() [2/2]

uint256_t & uint256_t::operator%= ( const uint256_t & p)
inline

operator %= for uint256_t

Parameters
p256-bit unsigned integer
Returns
this set as unsigned 256-bit remainder.

Definition at line 511 of file uint256_t.hpp.

511 {
512 *this = divide(p).second;
513 return *this;
514 }

◆ operator&() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t uint256_t::operator& ( const T & p)
inline

operator & for other types (bitwise operator)

Template Parameters
Tintegral type
Parameters
pnumber to be operated
Returns
value of this & p (& is bit-wise operator)

Definition at line 802 of file uint256_t.hpp.

802 {
803 return *this & uint256_t(p);
804 }

◆ operator&() [2/2]

uint256_t uint256_t::operator& ( const uint256_t & p)
inline

operator & for uint256_t (bitwise operator)

Parameters
pnumber to be operated
Returns
value of this & p (& is bit-wise operator)

Definition at line 811 of file uint256_t.hpp.

811 {
812 return {f & p.f, s & p.s};
813 }

◆ operator&&() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint256_t::operator&& ( const T & b)
inline

operator && for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is both values are non-zero, else false

Definition at line 693 of file uint256_t.hpp.

693 {
694 return (s || f) && (b);
695 }

◆ operator&&() [2/2]

bool uint256_t::operator&& ( const uint256_t & b)
inline

operator && for uint256_t

Parameters
bnumber to be compared with this
Returns
true if both of the values are not zero, else false

Definition at line 594 of file uint256_t.hpp.

594 {
595 return (s || f) && (b.s || b.f);
596 }

◆ operator&=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & uint256_t::operator&= ( const T p)
inline

operator &= for other types (bitwise operator)

Template Parameters
Tintegral type
Parameters
pnumber to be operated
Returns
this = this & p (& is bit-wise operator)

Definition at line 834 of file uint256_t.hpp.

834 {
835 s &= p.s;
836 return *this;
837 }

◆ operator&=() [2/2]

uint256_t & uint256_t::operator&= ( const uint256_t & p)
inline

operator &= for uint256_t (bitwise operator)

Parameters
pnumber to be operated
Returns
this = this & p (& is bit-wise operator)

Definition at line 820 of file uint256_t.hpp.

820 {
821 f &= p.f;
822 s &= p.s;
823 return *this;
824 }

◆ operator()()

bool uint256_t::operator() ( )
inline

operator () for uint256_t

Returns
true if this value is non-zero, else false

Definition at line 611 of file uint256_t.hpp.

611{ return s || f; }

◆ operator*() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t uint256_t::operator* ( const T & p)
inline

operator * for uint256_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
multiplication of this and p, returning uint256_t integer

Definition at line 353 of file uint256_t.hpp.

353 {
354 return *this * uint256_t(p);
355 }

◆ operator*() [2/2]

uint256_t uint256_t::operator* ( const uint256_t & p)
inline

operator * for uint256_t and other integer types.

Parameters
p256-bit unsigned integer
Returns
multiplication of this and p, returning uint256_t integer

Definition at line 362 of file uint256_t.hpp.

362 {
363 uint128_t f_first(s.upper()), f_second(s.lower()), s_first(p.s.upper()),
364 s_second(p.s.lower());
365 uint128_t fi = f_first * s_first, se = f_first * s_second,
366 th = s_first * f_second, fo = s_second * f_second;
367 uint128_t tmp = se << 64, tmp2 = th << 64;
368 int cc = (tmp + tmp2 < tmp);
369 tmp += tmp2;
370 cc += (tmp + fo < tmp);
371 return {f * p.s + s * p.f + fi + se.upper() + th.upper() + cc,
372 tmp + fo};
373 }
uint64_t upper() const
returns upper 64-bit integer part
uint64_t lower() const
returns lower 64-bit integer part

◆ operator*=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & uint256_t::operator*= ( const T & p)
inline

operator *= for uint256_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
multiplication of this and p, returning this

Definition at line 383 of file uint256_t.hpp.

383 {
384 return (*this *= uint256_t(p));
385 }

◆ operator*=() [2/2]

uint256_t & uint256_t::operator*= ( const uint256_t & p)
inline

operator *= for uint256_t and other integer types.

Parameters
p256-bit unsigned integer
Returns
multiplication of this and p, returning this

Definition at line 392 of file uint256_t.hpp.

392 {
393 uint128_t f_first(s.upper()), f_second(s.lower()), s_first(p.s.upper()),
394 s_second(p.s.lower());
395 uint128_t fi = f_first * s_first, se = f_first * s_second,
396 th = s_first * f_second, fo = s_second * f_second;
397 uint128_t tmp = se << 64, tmp2 = th << 64;
398 int cc = (tmp + tmp2 < tmp);
399 tmp += tmp2;
400 cc += (tmp + fo < tmp);
401 f = f * p.s + s * p.f + fi + se.upper() + th.upper() + cc;
402 s = tmp + fo;
403 return *this;
404 }

◆ operator+() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t uint256_t::operator+ ( const T & p)
inline

operator + for uint256_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
addition of this and p, returning uint256_t integer

Definition at line 211 of file uint256_t.hpp.

211 {
212 bool app = s + p < s;
213 return uint256_t(f + app, s + p);
214 }

◆ operator+() [2/2]

uint256_t uint256_t::operator+ ( const uint256_t & p)
inline

operator + for uint256_t and other integer types.

Parameters
p256-bit unsigned integer
Returns
addition of this and p, returning uint256_t integer

Definition at line 221 of file uint256_t.hpp.

221 {
222 bool app = (s + p.s < s);
223 return {f + app + p.f, s + p.s};
224 }

◆ operator++() [1/2]

uint256_t & uint256_t::operator++ ( )
inline

pre-increment operator

Returns
incremented value of this.

Definition at line 257 of file uint256_t.hpp.

257 {
258 *this += 1;
259 return *this;
260 }

◆ operator++() [2/2]

uint256_t uint256_t::operator++ ( int )
inline

post-increment operator

Returns
incremented value of this.

Definition at line 266 of file uint256_t.hpp.

266 {
267 ++*this;
268 return *this;
269 }

◆ operator+=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & uint256_t::operator+= ( const T & p)
inline

operator += for uint256_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
addition of this and p, returning this

Definition at line 234 of file uint256_t.hpp.

234 {
235 bool app = (p + s < s);
236 this->f += app;
237 this->s += p;
238 return *this;
239 }

◆ operator+=() [2/2]

uint256_t & uint256_t::operator+= ( const uint256_t & p)
inline

operator += for uint256_t

Parameters
p256-bit unsigned integer
Returns
addition of this and p, returning this

Definition at line 246 of file uint256_t.hpp.

246 {
247 bool app = (s + p.s < s);
248 f = f + app + p.f;
249 s = s + p.s;
250 return *this;
251 }

◆ operator-() [1/3]

uint256_t uint256_t::operator- ( )
inline

operator - using twos complement

Returns
2's complement of this.

Definition at line 298 of file uint256_t.hpp.

298{ return ~*this + uint256_t(1); }

◆ operator-() [2/3]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t uint256_t::operator- ( const T & p)
inline

operator - for uint256_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
subtraction of this and p, returning uint256_t integer

Definition at line 279 of file uint256_t.hpp.

279 {
280 bool app = (p > s);
281 return uint256_t(f - app, s - p);
282 }

◆ operator-() [3/3]

uint256_t uint256_t::operator- ( const uint256_t & p)
inline

operator - for uint256_t

Parameters
pa type of integer variable
Returns
subtraction of this and p, returning uint256_t integer

Definition at line 289 of file uint256_t.hpp.

289 {
290 bool app = s < p.s;
291 return {f - p.f - app, s - p.s};
292 }

◆ operator--() [1/2]

uint256_t & uint256_t::operator-- ( )
inline

operator – (pre-decrement)

Returns
decremented value of this

Definition at line 304 of file uint256_t.hpp.

304 {
305 *this -= 1;
306 return *this;
307 }

◆ operator--() [2/2]

uint256_t uint256_t::operator-- ( int )
inline

operator – (post-decrement)

Returns
decremented value of this

Definition at line 313 of file uint256_t.hpp.

313 {
314 --*this;
315 return *this;
316 }

◆ operator-=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t uint256_t::operator-= ( const T p)
inline

operator -= for uint256_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
subtraction of this and p, returning this

Definition at line 326 of file uint256_t.hpp.

326 {
327 bool app = (p > s);
328 f = f - app;
329 s = s - p;
330 return *this;
331 }

◆ operator-=() [2/2]

uint256_t & uint256_t::operator-= ( const uint256_t & p)
inline

operator -= for uint256_t

Parameters
p256-bit unsigned integer
Returns
subtraction of this and p, returning this

Definition at line 338 of file uint256_t.hpp.

338 {
339 bool app = s < p.s;
340 f = f - app - p.f;
341 s = s - p.s;
342 return *this;
343 }

◆ operator/() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t uint256_t::operator/ ( const T & p)
inline

operator / for uint256_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
unsigned 256-bit quotient.

Definition at line 449 of file uint256_t.hpp.

449 {
450 uint256_t tmp = *this;
451 tmp /= uint256_t(p);
452 return tmp;
453 }

◆ operator/() [2/2]

uint256_t uint256_t::operator/ ( const uint256_t & p)
inline

operator / for uint256_t and other integer types.

Parameters
p256-bit unsigned integer
Returns
unsigned 256-bit quotient.

Definition at line 460 of file uint256_t.hpp.

460{ return divide(p).first; }

◆ operator/=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & uint256_t::operator/= ( const T & p)
inline

operator /= for uint256_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
this set as unsigned 256-bit quotient.

Definition at line 480 of file uint256_t.hpp.

480 {
481 *this /= uint256_t(p);
482 return *this;
483 }

◆ operator/=() [2/2]

uint256_t & uint256_t::operator/= ( const uint256_t & p)
inline

operator /= for uint256_t

Parameters
p256-bit unsigned integer
Returns
this set as unsigned 256-bit quotient.

Definition at line 467 of file uint256_t.hpp.

467 {
468 *this = divide(p).first;
469 return *this;
470 }

◆ operator<() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint256_t::operator< ( const T & other)
inline

operator < for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is less than other, else false

Definition at line 621 of file uint256_t.hpp.

621 {
622 return *this < uint256_t(other);
623 }

◆ operator<() [2/2]

bool uint256_t::operator< ( const uint256_t & other)
inline

operator < for uint256_t

Parameters
othernumber to be compared with this
Returns
true if this is less than other, else false

Definition at line 534 of file uint256_t.hpp.

534 {
535 return f < other.f || (f == other.f && s < other.s);
536 }

◆ operator<<()

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t uint256_t::operator<< ( const T & p)
inline

operator << for uint256_t

Template Parameters
Tintegral type
Parameters
pnumber denoting number of shifts
Returns
value of this shifted by p to left

Definition at line 724 of file uint256_t.hpp.

724 {
725 if (!p) {
726 return {this->f, this->s};
727 } else if (p >= 128) {
728 return uint256_t((this->s << (p - 128)), uint128_t(0));
729 }
730 return uint256_t((this->f << p) + (this->s >> (128 - p)),
731 (this->s << p));
732 }

◆ operator<<=()

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & uint256_t::operator<<= ( const T & p)
inline

operator <<= for uint256_t

Template Parameters
Tintegral type
Parameters
pnumber denoting number of shifts
Returns
this shifted by p to left

Definition at line 742 of file uint256_t.hpp.

742 {
743 if (p) {
744 if (p >= 128) {
745 this->f = (this->s << (p - 128));
746 this->s = uint128_t(0);
747 } else {
748 f = ((this->s >> (128 - p)) + (this->f << p));
749 s = (this->s << p);
750 }
751 }
752 return *this;
753 }

◆ operator<=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint256_t::operator<= ( const T & other)
inline

operator <= for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is less than or equal to other, else false

Definition at line 633 of file uint256_t.hpp.

633 {
634 return *this <= uint256_t(other);
635 }

◆ operator<=() [2/2]

bool uint256_t::operator<= ( const uint256_t & other)
inline

operator <= for uint256_t

Parameters
othernumber to be compared with this
Returns
true if this is less than or equal to other, else false

Definition at line 543 of file uint256_t.hpp.

543 {
544 return f < other.f || (f == other.f && s <= other.s);
545 }

◆ operator=() [1/3]

uint256_t & uint256_t::operator= ( const std::string & p)
inline

operator = for type string

Parameters
pa string to assign it's value to equivalent integer
Returns
this pointer with it's value equal to p

Definition at line 193 of file uint256_t.hpp.

193 {
195 return *this;
196 }

◆ operator=() [2/3]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & uint256_t::operator= ( const T & p)
inline

operator = for other types

Template Parameters
Tdenoting any integer type
Parameters
pan integer to assign it's value
Returns
this pointer with it's value equal to p

Definition at line 183 of file uint256_t.hpp.

183 {
184 this->s = p;
185 return *this;
186 }

◆ operator=() [3/3]

uint256_t & uint256_t::operator= ( const uint256_t & p)
inlinedefault

operator = for uint256_t

Parameters
pan 256-bit integer to assign it's value
Returns
this pointer with it's value equal to p

◆ operator==() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint256_t::operator== ( const T & other)
inline

operator == for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is equal to other, else false

Definition at line 669 of file uint256_t.hpp.

669 {
670 return *this == uint256_t(other);
671 }

◆ operator==() [2/2]

bool uint256_t::operator== ( const uint256_t & other)
inline

operator == for uint256_t

Parameters
othernumber to be compared with this
Returns
true if this is equal than other, else false

Definition at line 570 of file uint256_t.hpp.

570 {
571 return f == other.f && s == other.s;
572 }

◆ operator>() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint256_t::operator> ( const T & other)
inline

operator > for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is greater than other, else false

Definition at line 645 of file uint256_t.hpp.

645 {
646 return *this > uint256_t(other);
647 }

◆ operator>() [2/2]

bool uint256_t::operator> ( const uint256_t & other)
inline

operator > for uint256_t

Parameters
othernumber to be compared with this
Returns
true if this is greater than other, else false

Definition at line 552 of file uint256_t.hpp.

552 {
553 return f > other.f || (f == other.f && s > other.s);
554 }

◆ operator>=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint256_t::operator>= ( const T & other)
inline

operator >= for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is greater than or equal other, else false

Definition at line 657 of file uint256_t.hpp.

657 {
658 return *this >= uint256_t(other);
659 }

◆ operator>=() [2/2]

bool uint256_t::operator>= ( const uint256_t & other)
inline

operator >= for uint256_t

Parameters
othernumber to be compared with this
Returns
true if this is greater than or equal than other, else false

Definition at line 561 of file uint256_t.hpp.

561 {
562 return (f > other.f) || (f == other.f && s >= other.s);
563 }

◆ operator>>()

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t uint256_t::operator>> ( const T & p)
inline

operator >> for uint256_t

Template Parameters
Tintegral type
Parameters
pnumber denoting number of shifts
Returns
value of this shifted by p to right

Definition at line 763 of file uint256_t.hpp.

763 {
764 if (!p) {
765 return {this->f, this->s};
766 } else if (p >= 128) {
767 return uint256_t(uint128_t(0), (this->f >> (p - 128)));
768 }
769 return uint256_t((this->f >> p),
770 (this->s >> p) + (this->f << (128 - p)));
771 }

◆ operator>>=()

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & uint256_t::operator>>= ( const T & p)
inline

operator >>= for uint256_t

Template Parameters
Tintegral type
Parameters
pnumber denoting number of shifts
Returns
this shifted by p to right

Definition at line 781 of file uint256_t.hpp.

781 {
782 if (p) {
783 if (p >= 128) {
784 f = uint128_t(0);
785 s = (this->f >> (p - 128));
786 } else {
787 s = (this->s >> p) + (this->f << (128 - p));
788 f = (this->f >> p);
789 }
790 }
791 return *this;
792 }

◆ operator^() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t uint256_t::operator^ ( const T & p)
inline

operator ^ for other types (bitwise operator)

Template Parameters
Tintegral type
Parameters
pnumber to be operated
Returns
value of this ^ p (^ is bit-wise XOR operator)

Definition at line 892 of file uint256_t.hpp.

892 {
893 return uint256_t(f, s ^ p);
894 }

◆ operator^() [2/2]

uint256_t uint256_t::operator^ ( const uint256_t & p)
inline

operator ^ for uint256_t (bitwise operator)

Parameters
pnumber to be operated
Returns
value of this ^ p (^ is bit-wise XOR operator)

Definition at line 901 of file uint256_t.hpp.

901 {
902 return {this->f ^ p.f, this->s ^ p.s};
903 }

◆ operator^=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & uint256_t::operator^= ( const T & p)
inline

operator ^= for other types (bitwise operator)

Template Parameters
Tintegral type
Parameters
pnumber to be operated
Returns
this = this ^ p (^ is bit-wise XOR operator)

Definition at line 924 of file uint256_t.hpp.

924 {
925 s ^= p;
926 return *this;
927 }

◆ operator^=() [2/2]

uint256_t & uint256_t::operator^= ( const uint256_t & p)
inline

operator ^= for uint256_t (bitwise operator)

Parameters
pnumber to be operated
Returns
this = this ^ p (^ is bit-wise XOR operator)

Definition at line 910 of file uint256_t.hpp.

910 {
911 f ^= p.f;
912 s ^= p.s;
913 return *this;
914 }

◆ operator|() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t uint256_t::operator| ( const T & p)
inline

operator | for other types (bitwise operator)

Template Parameters
Tintegral type
Parameters
pnumber to be operated
Returns
value of this | p (| is bit-wise operator)

Definition at line 847 of file uint256_t.hpp.

847 {
848 return *this | uint256_t(p);
849 }

◆ operator|() [2/2]

uint256_t uint256_t::operator| ( const uint256_t & p)
inline

operator | for uint256_t (bitwise operator)

Parameters
pnumber to be operated
Returns
value of this | p (| is bit-wise OR operator)

Definition at line 856 of file uint256_t.hpp.

856 {
857 return {this->f | p.f, this->s | p.s};
858 }

◆ operator|=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_t & uint256_t::operator|= ( const T & p)
inline

operator |= for other types (bitwise operator)

Template Parameters
Tintegral type
Parameters
pnumber to be operated
Returns
this = this | p (| is bit-wise OR operator)

Definition at line 868 of file uint256_t.hpp.

868 {
869 s |= p;
870 return *this;
871 }

◆ operator|=() [2/2]

uint256_t & uint256_t::operator|= ( const uint256_t & p)
inline

operator |= for uint256_t (bitwise operator)

Parameters
pnumber to be operated
Returns
this = this | p (| is bit-wise OR operator)

Definition at line 878 of file uint256_t.hpp.

878 {
879 f |= p.f;
880 s |= p.s;
881 return *this;
882 }

◆ operator||() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint256_t::operator|| ( const T & b)
inline

operator || for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is either one of the values are non-zero, else false

Definition at line 706 of file uint256_t.hpp.

706 {
707 return (s || f) || (b);
708 }

◆ operator||() [2/2]

bool uint256_t::operator|| ( const uint256_t & b)
inline

operator || for uint256_t

Parameters
bnumber to be compared with this
Returns
true if one of the values are not zero, else false

Definition at line 603 of file uint256_t.hpp.

603 {
604 return (s || f) || (b.s || b.f);
605 }

◆ operator~()

uint256_t uint256_t::operator~ ( )
inline

operator ~ for uint256_t

Returns
1's complement of this number

Definition at line 714 of file uint256_t.hpp.

714{ return {~f, ~s}; }

◆ upper()

uint128_t uint256_t::upper ( ) const
inline

returns upper 128-bit integer part

Returns
returns upper 128-bit integer part

Definition at line 166 of file uint256_t.hpp.

166{ return f; }

◆ operator<<

std::ostream & operator<< ( std::ostream & op,
uint256_t p )
friend

operator << for printing uint256_t integer

Prints the uint256_t integer in decimal form

Note
Note that this operator is costly since it uses strings to print the value
Parameters
opostream object
p256-bit integer
Returns
op, ostream object.

Definition at line 938 of file uint256_t.hpp.

938 {
939 if (!p.f) {
940 op << p.s;
941 } else {
942 std::string out = "0", p_2 = "1";
943 uint128_t L(1);
944 for (uint64_t i = 0; i < 128; ++i) {
945 if ((p.s & L)) {
946 out = add(out, p_2);
947 }
948 p_2 = add(p_2, p_2);
949 L <<= 1;
950 }
951 L = uint128_t(1);
952 for (int i = 0; i < 128; ++i) {
953 if ((p.f & L)) {
954 out = add(out, p_2);
955 }
956 p_2 = add(p_2, p_2);
957 L <<= 1;
958 }
959 op << out;
960 }
961 return op;
962 }
std::string add(const std::string &first, const std::string &second)
Adding two string.
Definition uint128_t.hpp:31

Member Data Documentation

◆ f

uint128_t uint256_t::f {}
private

Definition at line 25 of file uint256_t.hpp.

◆ s

uint128_t uint256_t::s {}
private

Definition at line 25 of file uint256_t.hpp.


The documentation for this class was generated from the following file: