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

class for 128-bit unsigned integer More...

#include <uint128_t.hpp>

Public Member Functions

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
 uint128_t (T low)
 Parameterized constructor.
 uint128_t (const std::string &str)
 Parameterized constructor.
 uint128_t (const uint64_t high, const uint64_t low)
 Parameterized constructor.
 uint128_t (const uint128_t &num)=default
 Copy constructor.
 uint128_t (uint128_t &&num) noexcept
 Move constructor.
 ~uint128_t ()=default
 Destructor for uint128_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 valu
uint64_t lower () const
 returns lower 64-bit integer part
uint64_t upper () const
 returns upper 64-bit integer part
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & operator= (const T &p)
 operator = for other types
uint128_t & operator= (const std::string &p)
 operator = for type string
uint128_t & operator= (const uint128_t &p)=default
 operator = for uint128_t
uint128_t & operator= (uint128_t &&p)=default
 Move assignment operator.
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator+ (const T p)
 operator + for uint128_t and other integer types.
uint128_t operator+ (const uint128_t &p)
 operator + for uint128_t and other integer types.
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & operator+= (const T p)
 operator += for uint128_t and other integer types.
uint128_t & operator+= (const uint128_t &p)
 operator += for uint128_t
uint128_t & operator++ ()
 pre-increment operator
uint128_t operator++ (int)
 post-increment operator
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator- (const T &p)
 operator - for uint128_t and other integer types.
uint128_t operator- (const uint128_t &p)
 operator - for uint128_t
uint128_t operator- ()
 operator - using twos complement
uint128_t & operator-- ()
 operator – (pre-decrement)
uint128_t operator-- (int)
 operator – (post-decrement)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & operator-= (const T &p)
 operator -= for uint128_t and other integer types.
uint128_t & operator-= (const uint128_t &p)
 operator -= for uint128_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator* (const T p)
 operator * for uint128_t and other integer types.
uint128_t operator* (const uint128_t &p)
 operator * for uint128_t and other integer types.
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & operator*= (const T p)
 operator *= for uint128_t and other integer types.
uint128_t & operator*= (const uint128_t &p)
 operator *= for uint128_t and other integer types.
std::pair< uint128_t, uint128_t > divide (const uint128_t &p)
 divide function for uint128_t and other integer types.
uint128_t operator/ (const uint128_t &p)
 operator / for uint128_t and other integer types.
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator/ (const T p)
 operator / for uint128_t and other integer types.
uint128_t & operator/= (const uint128_t &p)
 operator /= for uint128_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & operator/= (const T p)
 operator /= for uint128_t and other integer types.
uint128_t operator% (const uint128_t &p)
 operator % for uint128_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator% (const T &p)
 operator % for uint128_t and other integer types.
uint128_t & operator%= (const uint128_t &p)
 operator %= for uint128_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & operator%= (const T &p)
 operator %= for uint128_t
bool operator< (const uint128_t &other)
 operator < for uint128_t
bool operator<= (const uint128_t &other)
 operator <= for uint128_t
bool operator> (const uint128_t &other)
 operator > for uint128_t
bool operator>= (const uint128_t &other)
 operator >= for uint128_t
bool operator== (const uint128_t &other)
 operator == for uint128_t
bool operator!= (const uint128_t &other)
 operator != for uint128_t
bool operator! ()
 operator ! for uint128_t
bool operator&& (const uint128_t &b)
 operator && for uint128_t
bool operator|| (const uint128_t &b)
 operator || for uint128_t
bool operator() ()
 operator () for uint128_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
uint128_t operator~ ()
 operator ~ for uint128_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator<< (const T p)
 operator << for uint128_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & operator<<= (const T p)
 operator <<= for uint128_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator>> (const T p)
 operator >> for uint128_t
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & operator>>= (const T p)
 operator >>= for uint128_t
uint128_t operator& (const uint128_t &p)
 operator & for uint128_t (bitwise operator)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator& (const T p)
 operator & for other types (bitwise operator)
uint128_t & operator&= (const uint128_t &p)
 operator &= for uint128_t (bitwise operator)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_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>
uint128_t operator| (const T p)
 operator | for other types (bitwise operator)
uint128_t operator| (const uint128_t &p)
 operator | for uint128_t (bitwise operator)
uint128_t & operator|= (const uint128_t &p)
 operator |= for uint128_t (bitwise operator)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_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>
uint128_t operator^ (const T p)
 operator ^ for other types (bitwise operator)
uint128_t operator^ (const uint128_t &p)
 operator ^ for uint128_t (bitwise operator)
uint128_t & operator^= (const uint128_t &p)
 operator ^= for uint128_t (bitwise operator)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_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 128 bit number.

Private Attributes

uint64_t f {}
uint64_t s {}

Friends

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

Detailed Description

class for 128-bit unsigned integer

Definition at line 53 of file uint128_t.hpp.

Constructor & Destructor Documentation

◆ uint128_t() [1/5]

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

Parameterized constructor.

Template Parameters
Tintegral type
Parameters
lowlower part 8-bit unisgned integer

Definition at line 94 of file uint128_t.hpp.

94: s(low) {}

◆ uint128_t() [2/5]

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

Parameterized constructor.

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

Definition at line 100 of file uint128_t.hpp.

100 {
102 }
void __get_integer_from_string(const std::string &str)
First and second half of 128 bit number.
Definition uint128_t.hpp:63

◆ uint128_t() [3/5]

uint128_t::uint128_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 109 of file uint128_t.hpp.

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

◆ uint128_t() [4/5]

uint128_t::uint128_t ( const uint128_t & num)
default

Copy constructor.

Parameters
num128-bit unsigned integer

◆ uint128_t() [5/5]

uint128_t::uint128_t ( uint128_t && num)
inlinenoexcept

Move constructor.

Parameters
num128-bit unsigned integer

Definition at line 121 of file uint128_t.hpp.

121: f(num.f), s(num.s) {}

Member Function Documentation

◆ __get_integer_from_string()

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

First and second half of 128 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 63 of file uint128_t.hpp.

63 {
64 this->f = this->s = 0;
65 if (str.size() > 1 && str[1] == 'x') { // if hexadecimal
66 for (auto i = 2; i < str.size(); ++i) {
67 *this *= 16LL;
68 if (str[i] >= '0' && str[i] <= '9') {
69 *this += (str[i] - '0');
70 } else if (str[i] >= 'A' && str[i] <= 'F') {
71 *this += (str[i] - 'A' + 10);
72 } else if (str[i] >= 'a' && str[i] <= 'f') {
73 *this += (str[i] - 'a' + 10);
74 }
75 }
76 } else { // if decimal
77 for (auto &x : str) {
78 *this *= 10LL;
79 *this += (x - '0');
80 }
81 }
82 }

◆ _lez()

uint32_t uint128_t::_lez ( )
inline

Leading zeroes in binary.

Calculates leading zeros in 128-bit integer

Returns
Integer denoting leading zeroes

Definition at line 133 of file uint128_t.hpp.

133 {
134#ifndef _MSC_VER
135 if (f) {
136 return __builtin_clzll(f);
137 }
138 return 64 + __builtin_clzll(s);
139#else
140 unsigned long r = 0;
141 _BitScanForward64(&r, f);
142 if (r == 64) {
143 unsigned long l = 0;
144 _BitScanForward64(&l, s);
145 return 64 + l;
146 }
147 return r;
148#endif
149 }
double l(double x)
Another test function.

◆ _trz()

uint32_t uint128_t::_trz ( )
inline

Trailing zeroes in binary.

Calculates leading zeros in 128-bit integer

Returns
Integer denoting Trailing zeroes

Definition at line 156 of file uint128_t.hpp.

156 {
157#ifndef _MSC_VER
158 if (f) {
159 return __builtin_ctzll(f);
160 }
161 return 64 + __builtin_ctzll(s);
162#else
163 unsigned long r = 0;
164 _BitScanReverse64(&r, s);
165 if (r == 64) {
166 unsigned long l = 0;
167 _BitScanReverse64(&l, f);
168 return 64 + l;
169 }
170 return r;
171#endif
172 }

◆ divide()

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

divide function for uint128_t and other integer types.

divide this value and

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

Definition at line 448 of file uint128_t.hpp.

448 {
449 if (*this < p) { // if this is less than divisor
450 return {uint128_t(0), *this};
451 } else if (*this == p) { // if this is equal to divisor
452 return {uint128_t(1), uint128_t(0)};
453 }
454 uint128_t tmp = p, tmp2 = *this;
455 uint16_t left = tmp._lez() - _lez();
456 tmp <<= left;
457 uint128_t quotient(0);
458 uint128_t zero(0);
459 while (tmp2 >= p) {
460 uint16_t shf = tmp2._lez() - tmp._lez();
461 if (shf) {
462 tmp >>= shf;
463 quotient <<= shf;
464 left -= shf;
465 }
466 if (tmp2 < tmp) {
467 tmp >>= 1;
468 quotient <<= 1;
469 --left;
470 }
471 tmp2 -= tmp;
472 ++quotient;
473 }
474 return {quotient << left, tmp2};
475 }
uint32_t _lez()
Leading zeroes in binary.

◆ lower()

uint64_t uint128_t::lower ( ) const
inline

returns lower 64-bit integer part

Returns
returns lower 64-bit integer part

Definition at line 195 of file uint128_t.hpp.

195{ return s; }

◆ operator bool()

uint128_t::operator bool ( ) const
inlineexplicit

casting operator to boolean value

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

Definition at line 178 of file uint128_t.hpp.

178{ return (f || s); }

◆ operator T()

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

casting operator to any integer valu

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

Definition at line 187 of file uint128_t.hpp.

187 {
188 return static_cast<T>(s);
189 }

◆ operator!()

bool uint128_t::operator! ( )
inline

operator ! for uint128_t

Returns
true if this has zero value, else false

Definition at line 621 of file uint128_t.hpp.

621{ return !f && !s; }

◆ operator!=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_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 715 of file uint128_t.hpp.

715 {
716 return *this != uint128_t(other);
717 }

◆ operator!=() [2/2]

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

operator != for uint128_t

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

Definition at line 613 of file uint128_t.hpp.

613 {
614 return f != other.f || s != other.s;
615 }

◆ operator%() [1/2]

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

operator % for uint128_t and other integer types.

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

Definition at line 536 of file uint128_t.hpp.

536 {
537 return *this % uint128_t(p);
538 }

◆ operator%() [2/2]

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

operator % for uint128_t

Parameters
p128-bit unsigned integer
Returns
unsigned 128-bit remainder.

Definition at line 526 of file uint128_t.hpp.

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

◆ operator%=() [1/2]

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

operator %= for uint128_t

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

Definition at line 558 of file uint128_t.hpp.

558 {
559 *this %= uint128_t(p);
560 return *this;
561 }

◆ operator%=() [2/2]

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

operator %= for uint128_t

Parameters
p128-bit unsigned integer
Returns
this set as unsigned 128-bit remainder.

Definition at line 545 of file uint128_t.hpp.

545 {
546 *this = divide(p).second;
547 return *this;
548 }

◆ operator&() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_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 849 of file uint128_t.hpp.

849 {
850 uint128_t tmp = *this;
851 return tmp & uint128_t(p);
852 }

◆ operator&() [2/2]

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

operator & for uint128_t (bitwise operator)

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

Definition at line 837 of file uint128_t.hpp.

837 {
838 return uint128_t(this->f & p.f, this->s & p.s);
839 }

◆ operator&&() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_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 727 of file uint128_t.hpp.

727 {
728 return (f || s) && b;
729 }

◆ operator&&() [2/2]

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

operator && for uint128_t

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

Definition at line 628 of file uint128_t.hpp.

628 {
629 return (s || f) && (b.s || b.f);
630 }

◆ operator&=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_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 873 of file uint128_t.hpp.

873 {
874 *this &= uint128_t(p);
875 return *this;
876 }

◆ operator&=() [2/2]

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

operator &= for uint128_t (bitwise operator)

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

Definition at line 859 of file uint128_t.hpp.

859 {
860 this->f &= p.f;
861 this->s &= p.s;
862 return *this;
863 }

◆ operator()()

bool uint128_t::operator() ( )
inline

operator () for uint128_t

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

Definition at line 645 of file uint128_t.hpp.

645{ return s || f; }

◆ operator*() [1/2]

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

operator * for uint128_t and other integer types.

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

Definition at line 386 of file uint128_t.hpp.

386 {
387 return *this * uint128_t(p);
388 }

◆ operator*() [2/2]

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

operator * for uint128_t and other integer types.

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

Definition at line 395 of file uint128_t.hpp.

395 {
396 uint64_t f_first = s >> 32, f_second = s & 0xFFFFFFFF,
397 s_first = p.s >> 32, s_second = p.s & 0xFFFFFFFF;
398 uint64_t fi = f_first * s_first, se = f_first * s_second,
399 th = s_first * f_second, fo = s_second * f_second;
400 uint64_t tmp = ((se & 0xFFFFFFFF) << 32), tmp2 = (th & 0xFFFFFFFF)
401 << 32;
402 int cc = (tmp + tmp2 < tmp);
403 tmp += tmp2;
404 cc += (tmp + fo < tmp);
405 uint64_t carry = fi + (se >> 32) + (th >> 32);
406 return uint128_t(this->f * p.s + this->s * p.f + carry + cc, tmp + fo);
407 }

◆ operator*=() [1/2]

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

operator *= for uint128_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 417 of file uint128_t.hpp.

417 {
418 *this *= uint128_t(p);
419 return *this;
420 }

◆ operator*=() [2/2]

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

operator *= for uint128_t and other integer types.

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

Definition at line 427 of file uint128_t.hpp.

427 {
428 uint64_t f_first = s >> 32, f_second = s & 0xFFFFFFFF,
429 s_first = p.s >> 32, s_second = p.s & 0xFFFFFFFF;
430 uint64_t fi = f_first * s_first, se = f_first * s_second,
431 th = s_first * f_second, fo = s_second * f_second;
432 uint64_t tmp = (se << 32), tmp2 = (th << 32);
433 int cc = (tmp + tmp2 < tmp);
434 tmp += tmp2;
435 cc += (tmp + fo < tmp);
436 uint64_t carry = fi + (se >> 32) + (th >> 32);
437 f = this->f * p.s + this->s * p.f + carry + cc;
438 s = tmp + fo;
439 return *this;
440 }

◆ operator+() [1/2]

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

operator + for uint128_t and other integer types.

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

Definition at line 246 of file uint128_t.hpp.

246 {
247 return uint128_t(f + (p + s < s), p + s);
248 }

◆ operator+() [2/2]

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

operator + for uint128_t and other integer types.

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

Definition at line 255 of file uint128_t.hpp.

255 {
256 return uint128_t(f + (p.s + s < s) + p.f, p.s + s);
257 }

◆ operator++() [1/2]

uint128_t & uint128_t::operator++ ( )
inline

pre-increment operator

Returns
incremented value of this.

Definition at line 290 of file uint128_t.hpp.

290 {
291 *this += 1;
292 return *this;
293 }

◆ operator++() [2/2]

uint128_t uint128_t::operator++ ( int )
inline

post-increment operator

Returns
incremented value of this.

Definition at line 299 of file uint128_t.hpp.

299 {
300 ++*this;
301 return *this;
302 }

◆ operator+=() [1/2]

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

operator += for uint128_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 267 of file uint128_t.hpp.

267 {
268 bool app = p + s < s;
269 this->f += app;
270 this->s += p;
271 return *this;
272 }

◆ operator+=() [2/2]

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

operator += for uint128_t

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

Definition at line 279 of file uint128_t.hpp.

279 {
280 bool app = p.s + s < s;
281 f = f + app + p.f;
282 s = p.s + s;
283 return *this;
284 }

◆ operator-() [1/3]

uint128_t uint128_t::operator- ( )
inline

operator - using twos complement

Returns
2's complement of this.

Definition at line 331 of file uint128_t.hpp.

331{ return ~*this + uint128_t(1); }

◆ operator-() [2/3]

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

operator - for uint128_t and other integer types.

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

Definition at line 312 of file uint128_t.hpp.

312 {
313 bool app = p > s;
314 return uint128_t(f - app, s - p);
315 }

◆ operator-() [3/3]

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

operator - for uint128_t

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

Definition at line 322 of file uint128_t.hpp.

322 {
323 bool app = p.s > s;
324 return uint128_t(f - p.f - app, s - p.s);
325 }

◆ operator--() [1/2]

uint128_t & uint128_t::operator-- ( )
inline

operator – (pre-decrement)

Returns
decremented value of this

Definition at line 337 of file uint128_t.hpp.

337 {
338 *this -= 1;
339 return *this;
340 }

◆ operator--() [2/2]

uint128_t uint128_t::operator-- ( int )
inline

operator – (post-decrement)

Returns
decremented value of this

Definition at line 346 of file uint128_t.hpp.

346 {
347 --*this;
348 return *this;
349 }

◆ operator-=() [1/2]

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

operator -= for uint128_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 359 of file uint128_t.hpp.

359 {
360 bool app = p > s;
361 f -= app;
362 s -= p;
363 return *this;
364 }

◆ operator-=() [2/2]

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

operator -= for uint128_t

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

Definition at line 371 of file uint128_t.hpp.

371 {
372 bool app = p.s > s;
373 f = f - p.f - app;
374 s = s - p.s;
375 return *this;
376 }

◆ operator/() [1/2]

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

operator / for uint128_t and other integer types.

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

Definition at line 492 of file uint128_t.hpp.

492 {
493 uint128_t tmp = *this;
494 tmp /= uint128_t(0, p);
495 return tmp;
496 }

◆ operator/() [2/2]

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

operator / for uint128_t and other integer types.

Parameters
p128-bit unsigned integer
Returns
unsigned 128-bit quotient.

Definition at line 482 of file uint128_t.hpp.

482{ return divide(p).first; }

◆ operator/=() [1/2]

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

operator /= for uint128_t and other integer types.

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

Definition at line 516 of file uint128_t.hpp.

516 {
517 *this /= uint128_t(0, p);
518 return *this;
519 }

◆ operator/=() [2/2]

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

operator /= for uint128_t

Parameters
p128-bit unsigned integer
Returns
this set as unsigned 128-bit quotient.

Definition at line 503 of file uint128_t.hpp.

503 {
504 *this = divide(p).first;
505 return *this;
506 }

◆ operator<() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_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 655 of file uint128_t.hpp.

655 {
656 return *this < uint128_t(other);
657 }

◆ operator<() [2/2]

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

operator < for uint128_t

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

Definition at line 568 of file uint128_t.hpp.

568 {
569 return f < other.f || (f == other.f && s < other.s);
570 }

◆ operator<<()

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

operator << for uint128_t

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

Definition at line 758 of file uint128_t.hpp.

758 {
759 if (!p) {
760 return uint128_t(f, s);
761 } else if (p >= 64 && p <= 128) {
762 return uint128_t((this->s << (p - 64)), 0);
763 } else if (p < 64 && p > 0) {
764 return uint128_t((this->f << p) + ((this->s >> (64 - p))),
765 this->s << p);
766 }
767 return uint128_t(0);
768 }

◆ operator<<=()

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

operator <<= for uint128_t

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

Definition at line 778 of file uint128_t.hpp.

778 {
779 if (p) {
780 if (p >= 64 && p <= 128) {
781 this->f = (this->s << (p - 64));
782 this->s = 0;
783 } else {
784 f = ((this->f << p) + (this->s >> (64 - p)));
785 s = (this->s << p);
786 }
787 }
788 return *this;
789 }

◆ operator<=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_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 667 of file uint128_t.hpp.

667 {
668 return *this <= uint128_t(other);
669 }

◆ operator<=() [2/2]

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

operator <= for uint128_t

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

Definition at line 577 of file uint128_t.hpp.

577 {
578 return f < other.f || (f == other.f && s <= other.s);
579 }

◆ operator=() [1/3]

uint128_t & uint128_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 221 of file uint128_t.hpp.

221 {
223 return *this;
224 }

◆ operator=() [2/3]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_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 211 of file uint128_t.hpp.

211 {
212 this->s = p;
213 return *this;
214 }

◆ operator=() [3/3]

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

operator = for uint128_t

Parameters
pan 128-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 uint128_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 703 of file uint128_t.hpp.

703 {
704 return *this == uint128_t(other);
705 }

◆ operator==() [2/2]

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

operator == for uint128_t

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

Definition at line 604 of file uint128_t.hpp.

604 {
605 return f == other.f && s == other.s;
606 }

◆ operator>() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_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 679 of file uint128_t.hpp.

679 {
680 return *this > uint128_t(other);
681 }

◆ operator>() [2/2]

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

operator > for uint128_t

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

Definition at line 586 of file uint128_t.hpp.

586 {
587 return f > other.f || (f == other.f && s > other.s);
588 }

◆ operator>=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_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 691 of file uint128_t.hpp.

691 {
692 return *this >= uint128_t(other);
693 }

◆ operator>=() [2/2]

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

operator >= for uint128_t

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

Definition at line 595 of file uint128_t.hpp.

595 {
596 return (f > other.f) || (f == other.f && s >= other.s);
597 }

◆ operator>>()

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

operator >> for uint128_t

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

Definition at line 799 of file uint128_t.hpp.

799 {
800 if (!p) {
801 return uint128_t(this->f, this->s);
802 } else if (p >= 64 && p <= 128) {
803 return uint128_t(0, (this->f >> (p - 64)));
804 } else if (p < 64 && p > 0) {
805 return uint128_t((this->f >> p),
806 (this->s >> p) + (this->f << (64 - p)));
807 }
808 return uint128_t(0);
809 }

◆ operator>>=()

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

operator >>= for uint128_t

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

Definition at line 819 of file uint128_t.hpp.

819 {
820 if (p) {
821 if (p >= 64) {
822 f = 0;
823 s = (this->f >> (p - 64));
824 } else {
825 s = (this->s >> p) + (this->f << (64 - p));
826 f = (this->f >> p);
827 }
828 }
829 return *this;
830 }

◆ operator^() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_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 931 of file uint128_t.hpp.

931 {
932 return uint128_t(this->f, this->s ^ p);
933 }

◆ operator^() [2/2]

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

operator ^ for uint128_t (bitwise operator)

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

Definition at line 940 of file uint128_t.hpp.

940 {
941 return uint128_t(this->f ^ p.f, this->s ^ p.s);
942 }

◆ operator^=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_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 963 of file uint128_t.hpp.

963 {
964 s ^= p;
965 return *this;
966 }

◆ operator^=() [2/2]

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

operator ^= for uint128_t (bitwise operator)

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

Definition at line 949 of file uint128_t.hpp.

949 {
950 f ^= p.f;
951 s ^= p.s;
952 return *this;
953 }

◆ operator|() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_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 886 of file uint128_t.hpp.

886 {
887 return uint128_t(p | s);
888 }

◆ operator|() [2/2]

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

operator | for uint128_t (bitwise operator)

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

Definition at line 895 of file uint128_t.hpp.

895 {
896 return uint128_t(this->f | p.f, this->s | p.s);
897 }

◆ operator|=() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_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 918 of file uint128_t.hpp.

918 {
919 s |= p.s;
920 return *this;
921 }

◆ operator|=() [2/2]

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

operator |= for uint128_t (bitwise operator)

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

Definition at line 904 of file uint128_t.hpp.

904 {
905 f |= p.f;
906 s |= p.s;
907 return *this;
908 }

◆ operator||() [1/2]

template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_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 740 of file uint128_t.hpp.

740 {
741 return (f || s) || b;
742 }

◆ operator||() [2/2]

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

operator || for uint128_t

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

Definition at line 637 of file uint128_t.hpp.

637 {
638 return (s || f) || (b.s || b.f);
639 }

◆ operator~()

uint128_t uint128_t::operator~ ( )
inline

operator ~ for uint128_t

Returns
1's complement of this number

Definition at line 748 of file uint128_t.hpp.

748{ return uint128_t(~this->f, ~this->s); }

◆ upper()

uint64_t uint128_t::upper ( ) const
inline

returns upper 64-bit integer part

Returns
returns upper 64-bit integer part

Definition at line 201 of file uint128_t.hpp.

201{ return f; }

◆ operator<<

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

operator << for printing uint128_t integer

Prints the uint128_t integer in decimal form

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

Definition at line 977 of file uint128_t.hpp.

977 {
978 if (!p.f) {
979 op << p.s;
980 } else {
981 std::string out = "0", p_2 = "1";
982 for (int i = 0; i < 64; ++i) {
983 if (p.s & (1LL << i)) {
984 out = add(out, p_2);
985 }
986 p_2 = add(p_2, p_2);
987 }
988 for (int i = 0; i < 64; ++i) {
989 if (p.f & (1LL << i)) {
990 out = add(out, p_2);
991 }
992 p_2 = add(p_2, p_2);
993 }
994 op << out;
995 }
996 return op;
997 }
std::string add(const std::string &first, const std::string &second)
Adding two string.
Definition uint128_t.hpp:31

Member Data Documentation

◆ f

uint64_t uint128_t::f {}
private

Definition at line 54 of file uint128_t.hpp.

◆ s

uint64_t uint128_t::s {}
private

Definition at line 54 of file uint128_t.hpp.


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