TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
uint128_t.hpp File Reference
#include <algorithm>
#include <cstdint>
#include <ostream>
#include <string>
#include <utility>
Include dependency graph for uint128_t.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  uint128_t
 class for 128-bit unsigned integer More...

Macros

#define CIPHERS_UINT128_T_HPP_
 for std::reverse and other operations

Functions

std::string add (const std::string &first, const std::string &second)
 Adding two string.
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator+ (const T &p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator- (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator* (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator/ (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator% (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator& (const T &p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator| (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator^ (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator&& (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator|| (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator== (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator!= (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator< (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator<= (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator> (const T p, const uint128_t &q)
template<typename T, typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator>= (const T p, const uint128_t &q)

Detailed Description

Implementation of 128-bit unsigned integers.

Note
The implementation can be flagged as not completed. This header is used with enough operations as a part of bigger integer types 256-bit integer.
Author
Ashish Daulatabad

Definition in file uint128_t.hpp.

Macro Definition Documentation

◆ CIPHERS_UINT128_T_HPP_

#define CIPHERS_UINT128_T_HPP_

for std::reverse and other operations

for std::cout overload for std::string for std::pair library

Definition at line 21 of file uint128_t.hpp.

Function Documentation

◆ add()

std::string add ( const std::string & first,
const std::string & second )

Adding two string.

Adds two long integer, only used for printing numbers

Parameters
firstFirst integer string
secondSecond integer string
Returns
string denoting the addition of both the strings

Definition at line 31 of file uint128_t.hpp.

31 {
32 std::string third;
33 int16_t sum = 0, carry = 0;
34 for (int32_t i = static_cast<int32_t>(first.size()) - 1,
35 j = static_cast<int32_t>(second.size()) - 1;
36 i >= 0 || j >= 0; --i, --j) {
37 sum = ((i >= 0 ? first[i] - '0' : 0) + (j >= 0 ? second[j] - '0' : 0) +
38 carry);
39 carry = sum / 10;
40 sum %= 10;
41 third.push_back(sum + '0');
42 }
43 if (carry) {
44 third.push_back('1');
45 }
46 std::reverse(third.begin(), third.end());
47 return third;
48}
T sum(const std::vector< std::valarray< T > > &A)

◆ operator!=()

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

Definition at line 1072 of file uint128_t.hpp.

1072 {
1073 return uint128_t(p) != q;
1074}
class for 128-bit unsigned integer
Definition uint128_t.hpp:53

◆ operator%()

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

Definition at line 1027 of file uint128_t.hpp.

1027 {
1028 return uint128_t(p) % q;
1029}

◆ operator&()

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

Definition at line 1034 of file uint128_t.hpp.

1034 {
1035 return uint128_t(p) & q;
1036}

◆ operator&&()

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

Definition at line 1053 of file uint128_t.hpp.

1053 {
1054 return uint128_t(p) && q;
1055}

◆ operator*()

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

Definition at line 1015 of file uint128_t.hpp.

1015 {
1016 return uint128_t(p) * q;
1017}

◆ operator+()

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

Definition at line 1003 of file uint128_t.hpp.

1003 {
1004 return uint128_t(p) + q;
1005}

◆ operator-()

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

Definition at line 1009 of file uint128_t.hpp.

1009 {
1010 return uint128_t(p) - q;
1011}

◆ operator/()

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

Definition at line 1021 of file uint128_t.hpp.

1021 {
1022 return uint128_t(p) / q;
1023}

◆ operator<()

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

Definition at line 1078 of file uint128_t.hpp.

1078 {
1079 return uint128_t(p) < q;
1080}

◆ operator<=()

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

Definition at line 1084 of file uint128_t.hpp.

1084 {
1085 return uint128_t(p) <= q;
1086}

◆ operator==()

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

Definition at line 1066 of file uint128_t.hpp.

1066 {
1067 return uint128_t(p) == q;
1068}

◆ operator>()

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

Definition at line 1090 of file uint128_t.hpp.

1090 {
1091 return uint128_t(p) > q;
1092}

◆ operator>=()

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

Definition at line 1096 of file uint128_t.hpp.

1096 {
1097 return uint128_t(p) >= q;
1098}

◆ operator^()

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

Definition at line 1046 of file uint128_t.hpp.

1046 {
1047 return uint128_t(p) ^ q;
1048}

◆ operator|()

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

Definition at line 1040 of file uint128_t.hpp.

1040 {
1041 return uint128_t(p) | q;
1042}

◆ operator||()

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

Definition at line 1059 of file uint128_t.hpp.

1059 {
1060 return uint128_t(p) || q;
1061}