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_toperator= (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_toperator= (const T &p)
 operator = for other types
 
uint256_toperator= (const std::string &p)
 operator = for type string
 
uint256_toperator= (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_toperator+= (const T &p)
 operator += for uint256_t and other integer types.
 
uint256_toperator+= (const uint256_t &p)
 operator += for uint256_t
 
uint256_toperator++ ()
 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_toperator-- ()
 operator – (pre-decrement)
 
uint256_t operator-- (int p)
 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_toperator-= (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_toperator*= (const T &p)
 operator *= for uint256_t and other integer types.
 
uint256_toperator*= (const uint256_t &p)
 operator *= for uint256_t and other integer types.
 
std::pair< uint256_t, uint256_tdivide (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_toperator/= (const uint256_t &p)
 operator /= for uint256_t
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_toperator/= (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_toperator%= (const uint256_t &p)
 operator %= for uint256_t
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint256_toperator%= (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_toperator<<= (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_toperator>>= (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_toperator&= (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_toperator&= (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_toperator|= (const T &p)
 operator |= for other types (bitwise operator)
 
uint256_toperator|= (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_toperator^= (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_toperator^= (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 33 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 75 of file uint256_t.hpp.

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

◆ 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 81 of file uint256_t.hpp.

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

◆ 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 95 of file uint256_t.hpp.

96 : 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 103 of file uint256_t.hpp.

104 : 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 111 of file uint256_t.hpp.

111: 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 43 of file uint256_t.hpp.

43 {
44 this->f = this->s = uint128_t(0);
45 if (str.size() > 1 && str[1] == 'x') {
46 for (auto i = 2; i < str.size(); ++i) {
47 *this *= 16LL;
48 if (str[i] >= '0' && str[i] <= '9') {
49 *this += (str[i] - '0');
50 } else if (str[i] >= 'A' && str[i] <= 'F') {
51 *this += (str[i] - 'A' + 10);
52 } else if (str[i] >= 'a' && str[i] <= 'f') {
53 *this += (str[i] - 'a' + 10);
54 }
55 }
56 } else {
57 for (auto &x : str) {
58 *this *= 10LL;
59 *this += (x - '0');
60 }
61 }
62 }
class for 128-bit unsigned integer
Definition uint128_t.hpp:60

◆ _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 123 of file uint256_t.hpp.

123 {
124 if (f) {
125 return f._lez();
126 }
127 return 128 + s._lez();
128 }
uint32_t _lez()
Leading zeroes in binary.

◆ _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 135 of file uint256_t.hpp.

135 {
136 if (s) {
137 return s._trz();
138 }
139 return 128 + f._trz();
140 }
uint32_t _trz()
Trailing zeroes in binary.

◆ 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 421 of file uint256_t.hpp.

421 {
422 if (*this < p) { // if this is less than divisor
423 return {uint256_t(0), *this};
424 } else if (*this == p) { // if this is equal to divisor
425 return {uint256_t(1), uint256_t(0)};
426 }
427 uint256_t tmp = p, tmp2 = *this;
428 uint16_t left = tmp._lez() - _lez();
429 tmp <<= left;
430 uint256_t quotient(0);
431 uint256_t zero(0);
432 while (tmp2 >= p) {
433 uint16_t shf = tmp2._lez() - tmp._lez();
434 if (shf) {
435 tmp >>= shf;
436 quotient <<= shf;
437 left -= shf;
438 }
439 if (tmp2 < tmp) {
440 tmp >>= 1;
441 quotient <<= 1;
442 --left;
443 }
444 tmp2 -= tmp;
445 ++quotient;
446 }
447 return {quotient << left, tmp2};
448 }
class for 256-bit unsigned integer
Definition uint256_t.hpp:33
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 169 of file uint256_t.hpp.

169{ 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 146 of file uint256_t.hpp.

146{ 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 155 of file uint256_t.hpp.

155 {
156 return static_cast<T>(s);
157 }

◆ 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 163 of file uint256_t.hpp.

163{ return s; }

◆ operator!()

bool uint256_t::operator! ( )
inline

operator ! for uint256_t

Returns
true if this has zero value, else false

Definition at line 596 of file uint256_t.hpp.

596{ 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 690 of file uint256_t.hpp.

690 {
691 return *this != uint256_t(other);
692 }

◆ 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 588 of file uint256_t.hpp.

588 {
589 return !((*this) == other);
590 }

◆ 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 509 of file uint256_t.hpp.

509 {
510 uint256_t tmp = *this;
511 tmp %= uint256_t(p);
512 return tmp;
513 }

◆ 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 499 of file uint256_t.hpp.

499{ 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 533 of file uint256_t.hpp.

533 {
534 *this %= uint256_t(p);
535 return *this;
536 }

◆ 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 520 of file uint256_t.hpp.

520 {
521 *this = divide(p).second;
522 return *this;
523 }

◆ 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 811 of file uint256_t.hpp.

811 {
812 return *this & uint256_t(p);
813 }

◆ 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 820 of file uint256_t.hpp.

820 {
821 return {f & p.f, s & p.s};
822 }

◆ 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 702 of file uint256_t.hpp.

702 {
703 return (s || f) && (b);
704 }

◆ 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 603 of file uint256_t.hpp.

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

◆ 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 843 of file uint256_t.hpp.

843 {
844 s &= p.s;
845 return *this;
846 }

◆ 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 829 of file uint256_t.hpp.

829 {
830 f &= p.f;
831 s &= p.s;
832 return *this;
833 }

◆ operator()()

bool uint256_t::operator() ( )
inline

operator () for uint256_t

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

Definition at line 620 of file uint256_t.hpp.

620{ 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 362 of file uint256_t.hpp.

362 {
363 return *this * uint256_t(p);
364 }

◆ 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 371 of file uint256_t.hpp.

371 {
372 uint128_t f_first(s.upper()), f_second(s.lower()), s_first(p.s.upper()),
373 s_second(p.s.lower());
374 uint128_t fi = f_first * s_first, se = f_first * s_second,
375 th = s_first * f_second, fo = s_second * f_second;
376 uint128_t tmp = se << 64, tmp2 = th << 64;
377 int cc = (tmp + tmp2 < tmp);
378 tmp += tmp2;
379 cc += (tmp + fo < tmp);
380 return {f * p.s + s * p.f + fi + se.upper() + th.upper() + cc,
381 tmp + fo};
382 }
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 392 of file uint256_t.hpp.

392 {
393 return (*this *= uint256_t(p));
394 }

◆ 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 401 of file uint256_t.hpp.

401 {
402 uint128_t f_first(s.upper()), f_second(s.lower()), s_first(p.s.upper()),
403 s_second(p.s.lower());
404 uint128_t fi = f_first * s_first, se = f_first * s_second,
405 th = s_first * f_second, fo = s_second * f_second;
406 uint128_t tmp = se << 64, tmp2 = th << 64;
407 int cc = (tmp + tmp2 < tmp);
408 tmp += tmp2;
409 cc += (tmp + fo < tmp);
410 f = f * p.s + s * p.f + fi + se.upper() + th.upper() + cc;
411 s = tmp + fo;
412 return *this;
413 }

◆ 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 220 of file uint256_t.hpp.

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

◆ 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 230 of file uint256_t.hpp.

230 {
231 bool app = (s + p.s < s);
232 return {f + app + p.f, s + p.s};
233 }

◆ operator++() [1/2]

uint256_t & uint256_t::operator++ ( )
inline

pre-increment operator

Returns
incremented value of this.

Definition at line 266 of file uint256_t.hpp.

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

◆ operator++() [2/2]

uint256_t uint256_t::operator++ ( int )
inline

post-increment operator

Returns
incremented value of this.

Definition at line 275 of file uint256_t.hpp.

275 {
276 ++*this;
277 return *this;
278 }

◆ 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 243 of file uint256_t.hpp.

243 {
244 bool app = (p + s < s);
245 this->f += app;
246 this->s += p;
247 return *this;
248 }

◆ 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 255 of file uint256_t.hpp.

255 {
256 bool app = (s + p.s < s);
257 f = f + app + p.f;
258 s = s + p.s;
259 return *this;
260 }

◆ operator-() [1/3]

uint256_t uint256_t::operator- ( )
inline

operator - using twos complement

Returns
2's complement of this.

Definition at line 307 of file uint256_t.hpp.

307{ 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 288 of file uint256_t.hpp.

288 {
289 bool app = (p > s);
290 return uint256_t(f - app, s - p);
291 }

◆ 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 298 of file uint256_t.hpp.

298 {
299 bool app = s < p.s;
300 return {f - p.f - app, s - p.s};
301 }

◆ operator--() [1/2]

uint256_t & uint256_t::operator-- ( )
inline

operator – (pre-decrement)

Returns
decremented value of this

Definition at line 313 of file uint256_t.hpp.

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

◆ operator--() [2/2]

uint256_t uint256_t::operator-- ( int p)
inline

operator – (post-decrement)

Returns
decremented value of this

Definition at line 322 of file uint256_t.hpp.

322 {
323 --*this;
324 return *this;
325 }

◆ 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 335 of file uint256_t.hpp.

335 {
336 bool app = (p > s);
337 f = f - app;
338 s = s - p;
339 return *this;
340 }

◆ 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 347 of file uint256_t.hpp.

347 {
348 bool app = s < p.s;
349 f = f - app - p.f;
350 s = s - p.s;
351 return *this;
352 }

◆ 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 458 of file uint256_t.hpp.

458 {
459 uint256_t tmp = *this;
460 tmp /= uint256_t(p);
461 return tmp;
462 }

◆ 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 469 of file uint256_t.hpp.

469{ 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 489 of file uint256_t.hpp.

489 {
490 *this /= uint256_t(p);
491 return *this;
492 }

◆ 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 476 of file uint256_t.hpp.

476 {
477 *this = divide(p).first;
478 return *this;
479 }

◆ 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 630 of file uint256_t.hpp.

630 {
631 return *this < uint256_t(other);
632 }

◆ 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 543 of file uint256_t.hpp.

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

◆ 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 733 of file uint256_t.hpp.

733 {
734 if (!p) {
735 return {this->f, this->s};
736 } else if (p >= 128) {
737 return uint256_t((this->s << (p - 128)), uint128_t(0));
738 }
739 return uint256_t((this->f << p) + (this->s >> (128 - p)),
740 (this->s << p));
741 }

◆ 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 751 of file uint256_t.hpp.

751 {
752 if (p) {
753 if (p >= 128) {
754 this->f = (this->s << (p - 128));
755 this->s = uint128_t(0);
756 } else {
757 f = ((this->s >> (128 - p)) + (this->f << p));
758 s = (this->s << p);
759 }
760 }
761 return *this;
762 }

◆ 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 642 of file uint256_t.hpp.

642 {
643 return *this <= uint256_t(other);
644 }

◆ 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 552 of file uint256_t.hpp.

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

◆ 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 202 of file uint256_t.hpp.

202 {
204 return *this;
205 }

◆ 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 192 of file uint256_t.hpp.

192 {
193 this->s = p;
194 return *this;
195 }

◆ 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 678 of file uint256_t.hpp.

678 {
679 return *this == uint256_t(other);
680 }

◆ 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 579 of file uint256_t.hpp.

579 {
580 return f == other.f && s == other.s;
581 }

◆ 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 654 of file uint256_t.hpp.

654 {
655 return *this > uint256_t(other);
656 }

◆ 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 561 of file uint256_t.hpp.

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

◆ 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 666 of file uint256_t.hpp.

666 {
667 return *this >= uint256_t(other);
668 }

◆ 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 570 of file uint256_t.hpp.

570 {
571 return (f > other.f) || (f == other.f && s >= other.s);
572 }

◆ 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 772 of file uint256_t.hpp.

772 {
773 if (!p) {
774 return {this->f, this->s};
775 } else if (p >= 128) {
776 return uint256_t(uint128_t(0), (this->f >> (p - 128)));
777 }
778 return uint256_t((this->f >> p),
779 (this->s >> p) + (this->f << (128 - p)));
780 }

◆ 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 790 of file uint256_t.hpp.

790 {
791 if (p) {
792 if (p >= 128) {
793 f = uint128_t(0);
794 s = (this->f >> (p - 128));
795 } else {
796 s = (this->s >> p) + (this->f << (128 - p));
797 f = (this->f >> p);
798 }
799 }
800 return *this;
801 }

◆ 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 901 of file uint256_t.hpp.

901 {
902 return uint256_t(f, s ^ p);
903 }

◆ 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 910 of file uint256_t.hpp.

910 {
911 return {this->f ^ p.f, this->s ^ p.s};
912 }

◆ 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 933 of file uint256_t.hpp.

933 {
934 s ^= p;
935 return *this;
936 }

◆ 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 919 of file uint256_t.hpp.

919 {
920 f ^= p.f;
921 s ^= p.s;
922 return *this;
923 }

◆ 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 856 of file uint256_t.hpp.

856 {
857 return *this | uint256_t(p);
858 }

◆ 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 865 of file uint256_t.hpp.

865 {
866 return {this->f | p.f, this->s | p.s};
867 }

◆ 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 877 of file uint256_t.hpp.

877 {
878 s |= p;
879 return *this;
880 }

◆ 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 887 of file uint256_t.hpp.

887 {
888 f |= p.f;
889 s |= p.s;
890 return *this;
891 }

◆ 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 715 of file uint256_t.hpp.

715 {
716 return (s || f) || (b);
717 }

◆ 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 612 of file uint256_t.hpp.

612 {
613 return (s || f) || (b.s || b.f);
614 }

◆ operator~()

uint256_t uint256_t::operator~ ( )
inline

operator ~ for uint256_t

Returns
1's complement of this number

Definition at line 723 of file uint256_t.hpp.

723{ 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 175 of file uint256_t.hpp.

175{ return f; }

Friends And Related Symbol Documentation

◆ 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 947 of file uint256_t.hpp.

947 {
948 if (!p.f) {
949 op << p.s;
950 } else {
951 std::string out = "0", p_2 = "1";
952 uint128_t L(1);
953 for (uint64_t i = 0; i < 128; ++i) {
954 if ((p.s & L)) {
955 out = add(out, p_2);
956 }
957 p_2 = add(p_2, p_2);
958 L <<= 1;
959 }
960 L = uint128_t(1);
961 for (int i = 0; i < 128; ++i) {
962 if ((p.f & L)) {
963 out = add(out, p_2);
964 }
965 p_2 = add(p_2, p_2);
966 L <<= 1;
967 }
968 op << out;
969 }
970 return op;
971 }
std::string add(const std::string &first, const std::string &second)
Adding two string.
Definition uint128_t.hpp:38

Member Data Documentation

◆ f

uint128_t uint256_t::f {}
private

Definition at line 34 of file uint256_t.hpp.

34{}, s{};

◆ s

uint128_t uint256_t::s {}
private

Definition at line 34 of file uint256_t.hpp.

34{}, s{};

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