类型声明:

 class uint128
{
public:
uint128() :hi(), lo(){}
uint128(uint32_t l) :hi(), lo(l){}
uint128(int32_t l) :hi(-(l < )), lo(l){}
uint128(int64_t l) :hi(-(l < )), lo(l){}
uint128(uint64_t l) :hi(), lo(l){}
uint128(const std::string& s);
uint128(uint64_t _h, uint64_t _l)
:hi(_h), lo(_l){} bool operator == (const uint128& o)const{ return hi == o.hi && lo == o.lo; }
bool operator != (const uint128& o)const{ return hi != o.hi || lo != o.lo; }
bool operator < (const uint128& o)const { return (hi == o.hi) ? lo < o.lo : hi < o.hi; }
bool operator < (const int64_t& o)const { return *this < uint128(o); }
bool operator !()const { return !(hi != || lo != ); }
uint128 operator -()const { return ++uint128(~hi, ~lo); }
uint128 operator ~()const { return uint128(~hi, ~lo); } uint128& operator++() { hi += (++lo == ); return *this; }
uint128& operator--() { hi -= (lo-- == ); return *this; }
uint128 operator++(int) { auto tmp = *this; ++(*this); return tmp; }
uint128 operator--(int) { auto tmp = *this; --(*this); return tmp; } uint128& operator |= (const uint128& u) { hi |= u.hi; lo |= u.lo; return *this; }
uint128& operator &= (const uint128& u) { hi &= u.hi; lo &= u.lo; return *this; }
uint128& operator ^= (const uint128& u) { hi ^= u.hi; lo ^= u.lo; return *this; }
uint128& operator <<= (const uint128& u);
uint128& operator >>= (const uint128& u); uint128& operator += (const uint128& u) { const uint64_t old = lo; lo += u.lo; hi += u.hi + (lo < old); return *this; }
uint128& operator -= (const uint128& u) { return *this += -u; }
uint128& operator *= (const uint128& u); friend uint128 operator + (const uint128& l, const uint128& r) { return uint128(l) += r; }
friend uint128 operator + (const uint128& l, const uint64_t& r) { return uint128(l) += uint128(r); }
friend uint128 operator + (const uint128& l, const uint32_t& r) { return uint128(l) += uint128(r); }
friend uint128 operator + (const uint128& l, const int32_t& r) { return uint128(l) += uint128(r); }
friend uint128 operator + (const uint64_t& l, const uint128& r) { return uint128(l) += r; }
friend uint128 operator - (const uint128& l, const uint128& r) { return uint128(l) -= r; }
friend uint128 operator * (const uint128& l, const uint128& r) { return uint128(l) *= r; }
friend uint128 operator * (const uint128& l, const uint64_t& r) { return uint128(l) *= uint128(r); }
friend uint128 operator * (const uint128& l, const uint32_t& r) { return uint128(l) *= uint128(r); }
friend uint128 operator | (const uint128& l, const uint128& r) { return uint128(l) = (r); }
friend uint128 operator & (const uint128& l, const uint128& r) { return uint128(l) &= r; }
friend uint128 operator & (const uint128& l, const uint64_t& r) { return uint128(l) &= uint128(r); }
friend uint128 operator ^ (const uint128& l, const uint128& r) { return uint128(l) ^= r; }
friend uint128 operator << (const uint128& l, const uint128& r) { return uint128(l) <<= r; }
friend uint128 operator >> (const uint128& l, const uint128& r) { return uint128(l) >>= r; }
friend uint128 operator >> (const uint128& l, const int32_t& r) { return uint128(l) >>= uint128(r); }
friend bool operator > (const uint128& l, const uint128& r) { return r < l; }
friend bool operator >(const uint128& l, const int64_t& r) { return uint128(r) < l; }
friend bool operator > (const int64_t& l, const uint128& r) { return r < uint128(l); } friend bool operator >= (const uint128& l, const uint128& r) { return l == r || l > r; }
friend bool operator >= (const uint128& l, const int64_t& r) { return l >= uint128(r); }
friend bool operator >= (const int64_t& l, const uint128& r) { return uint128(l) >= r; }
friend bool operator <= (const uint128& l, const uint128& r) { return l == r || l < r; }
friend bool operator <= (const uint128& l, const int64_t& r) { return l <= uint128(r); }
friend bool operator <= (const int64_t& l, const uint128& r) { return uint128(l) <= r; } operator uint64_t() { return lo; } //强制转换为uint64_t
operator uint32_t() { return (uint32_t)lo; } //强制转换为uint32_t
operator int32_t() { return (int32_t)lo; } //强制转换为int32_t uint32_t low_32_bits()const { return (uint32_t)lo; }
uint64_t low_bits()const { return lo; }
uint64_t high_bits()const { return hi; } uint64_t hi;
uint64_t lo;
};

实现函数:

 uint128::uint128(const std::string &sz)
:hi(), lo()
{
// do we have at least one character?
if (!sz.empty()) {
// make some reasonable assumptions
int radix = ;
bool minus = false; std::string::const_iterator i = sz.begin(); // check for minus sign, i suppose technically this should only apply
// to base 10, but who says that -0x1 should be invalid?
if (*i == '-') {
++i;
minus = true;
} // check if there is radix changing prefix (0 or 0x)
if (i != sz.end()) {
if (*i == '') {
radix = ;
++i;
if (i != sz.end()) {
if (*i == 'x') {
radix = ;
++i;
}
}
} while (i != sz.end()) {
unsigned int n = ;
const char ch = *i; if (ch >= 'A' && ch <= 'Z') {
if (((ch - 'A') + ) < radix) {
n = (ch - 'A') + ;
}
else {
break;
}
}
else if (ch >= 'a' && ch <= 'z') {
if (((ch - 'a') + ) < radix) {
n = (ch - 'a') + ;
}
else {
break;
}
}
else if (ch >= '' && ch <= '') {
if ((ch - '') < radix) {
n = (ch - '');
}
else {
break;
}
}
else {
/* completely invalid character */
break;
} (*this) *= radix;
(*this) += n; ++i;
}
} if (minus) {
*this = -*this;
}
}
} uint128& uint128::operator<<=(const uint128& rhs)
{
if (rhs >= )
{
hi = ;
lo = ;
}
else
{
unsigned int n = rhs.to_integer();
const unsigned int halfsize = / ; if (n >= halfsize){
n -= halfsize;
hi = lo;
lo = ;
} if (n != ) {
// shift high half
hi <<= n; const uint64_t mask(~(uint64_t(-) >> n)); // and add them to high half
hi |= (lo & mask) >> (halfsize - n); // and finally shift also low half
lo <<= n;
}
} return *this;
} uint128 & uint128::operator>>=(const uint128& rhs)
{
if (rhs >= )
{
hi = ;
lo = ;
}
else
{
unsigned int n = rhs.to_integer();
const unsigned int halfsize = / ; if (n >= halfsize) {
n -= halfsize;
lo = hi;
hi = ;
} if (n != ) {
// shift low half
lo >>= n; // get lower N bits of high half
const uint64_t mask(~(uint64_t(-) << n)); // and add them to low qword
lo |= (hi & mask) << (halfsize - n); // and finally shift also high half
hi >>= n;
}
}
return *this;
} uint128& uint128::operator*=(const uint128 &b)
{
uint64_t a0 = (uint32_t)(this->lo);
uint64_t a1 = (uint32_t)(this->lo >> 0x20);
uint64_t a2 = (uint32_t)(this->hi);
uint64_t a3 = (uint32_t)(this->hi >> 0x20); uint64_t b0 = (uint32_t)(b.lo);
uint64_t b1 = (uint32_t)(b.lo >> 0x20);
uint64_t b2 = (uint32_t)(b.hi);
uint64_t b3 = (uint32_t)(b.hi >> 0x20); this->hi = ;
this->lo = a3*b0;
(*this) += a2*b1;
(*this) += a1*b2;
(*this) += a0*b3;
(*this) <<= 0x20;
(*this) += a2*b0;
(*this) += a1*b1;
(*this) += a0*b2;
(*this) <<= 0x20;
(*this) += a1*b0;
(*this) += a0*b1;
(*this) <<= 0x20;
(*this) += a0*b0; return *this;
}

uint128_t 添加 c++ 重载类型强制转换的更多相关文章

  1. C#高级编程9-第7章 运算符和类型强制转换

    运算符和类型强制转换 1.运算符 运算符的简化操作 条件运算符: if-else的简化操作,也称三元运算符.如果条件为真,返回一个值,为假返回另外一个值. condition?true_value:f ...

  2. C#高级编程 (第六版) 学习 第六章:运算符和类型强制转换

    第六章 运算符和类型强制转换 1,运算符 类别 运算符 算术运算符 + - * / % 逻辑运算符 & | ^ ~ && || ! 字符串连接运算符 + 增量和减量运算符 ++ ...

  3. C#学习笔记二 (资源托管,泛型,数组和元组,运算符和类型强制转换)

     托管和非托管资源 1.托管资源是指GC管理的内存空间,非托管资源是指文件句柄,网络连接,数据库连接等. 2.方法中临时申请的变量,被存放在栈中.栈存储非对象成员的值数据.例如在方法中有B b=new ...

  4. 3_PHP表达式_5_数据类型转换_类型强制转换

    以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. PHP类型转换分为类型自动转换和类型强制转换. 3.5.2 类型强制转换 类型强制转换允许编程人员手动将变量的数据 ...

  5. C# 运算符和类型强制转换(6) 持续更新

    C#支持的运算符 https://msdn.microsoft.com/zh-cn/library/6a71f45d(v=vs.140).aspx checked 和 unchecked ; b++; ...

  6. 【读书笔记】C#高级编程 第七章 运算符和类型强制转换

    (一)运算符 类别 运算符 算术运算符 + - * / % 逻辑运算符 & | ^ ~ && || ! 字符串连接运算符 + 增量和减量运算符 ++ -- 移位运算符 < ...

  7. C#高级编程笔记 2016年10月8日运算符和类型强制转换

    1.checked和unchecked 运算符 C#提供了checked 和uncheckde 运算符.如果把一个代码块标记为checked, CLR就会执行溢出检查,如果发生溢出,就抛出overfl ...

  8. C语言指针类型 强制转换

    关于C语言指针类型 强制转换  引用一篇文章: C语言中,任何一个变量都必须占有一个地址,而这个地址空间内的0-1代码就是这个变量的值.不同的数据类型占有的空间大小不一,但是他们都必须有个地址,而这个 ...

  9. OC中的类型强制转换

    在Objective-C中,以数字格式组成的字符串经常需要转换为NSNumber对象后再使用.例如有一个字符串对象@"111.22",需要转为NSNumber对象,最简单的方法就是 ...

随机推荐

  1. 剑指offer32:把数组排成最小的数

    1 题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 2 思路 ...

  2. 【php设计模式】单例模式

    实现单例的三个关键点: 1.使用一个静态成员来保持一个单例实例 2.一个私有的构造方法使得该类只能在类的内部方法中被实例化 3.在实例化对象的静态方法中,先判断静态变量是否已经被赋值,如果赋值则返回该 ...

  3. Algorithm negotiation failed

    #用pycharm工具ssh client 报 algorithm negotiation failed#导致原因:是ssh升级后,为了安全,默认不再采用原来一些加密算法,我们手工添加进去即可#目前出 ...

  4. 有关this指针指向问题

    在下面两个写法中 var obj = { foo: function () {} }; var foo = obj.foo; // 写法一 obj.foo() // 写法二 foo() 上面代码中,虽 ...

  5. 通过ADB调试安卓程序

    ADB,即 Android Debug Bridge,它是Android开发/测试人员不可替代的强大工具. 1.下载ADB后,将以下四个文件放到某个文件夹下即可.因为打开Cmd默认路径是 C:\Use ...

  6. 命令行发送SMTP协议邮件(163邮箱)

    这里我们用163邮箱为例子,借助命令行发送smtp邮件 1.连接服务器 在终端上输入:telnet smtp.163.com 25 回车,然后就连接了服务器的25端口,成功会输出 220 163.co ...

  7. Python算法题(三)——经典函数题

    题目一(统计字符串中指定类型字符的个数): 假设所有字符分为三类:字母,数字及其他字符.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬ ...

  8. oracle中查询表中的触发器,关闭启用操作

    1.查询指定表中有哪些触发器 select * from all_triggers WHERE table_name='表名' 2.禁用指定表中所有的触发器 alter table table_nam ...

  9. LeetCode 腾讯精选50题--二叉树中的最大路径和

    二叉树中的最大路径和 题目描述 给定一个非空二叉树,返回器最大路径和,路径指一条从任意节点出发,到达任意节点的序列,该路径至少包含一个节点,且不一定经过根节点 解题思路 树这一类数据结构我还不是很熟悉 ...

  10. script标签所应放的位置

    一般放置的位置:<head>标签内,<body>标签内,<body>标签后(建议放在body标签后,利于页面的优化,优化页面结构加载的速度) 1.<head& ...