类型声明:

  1. class uint128
  2. {
  3. public:
  4. uint128() :hi(), lo(){}
  5. uint128(uint32_t l) :hi(), lo(l){}
  6. uint128(int32_t l) :hi(-(l < )), lo(l){}
  7. uint128(int64_t l) :hi(-(l < )), lo(l){}
  8. uint128(uint64_t l) :hi(), lo(l){}
  9. uint128(const std::string& s);
  10. uint128(uint64_t _h, uint64_t _l)
  11. :hi(_h), lo(_l){}
  12.  
  13. bool operator == (const uint128& o)const{ return hi == o.hi && lo == o.lo; }
  14. bool operator != (const uint128& o)const{ return hi != o.hi || lo != o.lo; }
  15. bool operator < (const uint128& o)const { return (hi == o.hi) ? lo < o.lo : hi < o.hi; }
  16. bool operator < (const int64_t& o)const { return *this < uint128(o); }
  17. bool operator !()const { return !(hi != || lo != ); }
  18. uint128 operator -()const { return ++uint128(~hi, ~lo); }
  19. uint128 operator ~()const { return uint128(~hi, ~lo); }
  20.  
  21. uint128& operator++() { hi += (++lo == ); return *this; }
  22. uint128& operator--() { hi -= (lo-- == ); return *this; }
  23. uint128 operator++(int) { auto tmp = *this; ++(*this); return tmp; }
  24. uint128 operator--(int) { auto tmp = *this; --(*this); return tmp; }
  25.  
  26. uint128& operator |= (const uint128& u) { hi |= u.hi; lo |= u.lo; return *this; }
  27. uint128& operator &= (const uint128& u) { hi &= u.hi; lo &= u.lo; return *this; }
  28. uint128& operator ^= (const uint128& u) { hi ^= u.hi; lo ^= u.lo; return *this; }
  29. uint128& operator <<= (const uint128& u);
  30. uint128& operator >>= (const uint128& u);
  31.  
  32. uint128& operator += (const uint128& u) { const uint64_t old = lo; lo += u.lo; hi += u.hi + (lo < old); return *this; }
  33. uint128& operator -= (const uint128& u) { return *this += -u; }
  34. uint128& operator *= (const uint128& u);
  35.  
  36. friend uint128 operator + (const uint128& l, const uint128& r) { return uint128(l) += r; }
  37. friend uint128 operator + (const uint128& l, const uint64_t& r) { return uint128(l) += uint128(r); }
  38. friend uint128 operator + (const uint128& l, const uint32_t& r) { return uint128(l) += uint128(r); }
  39. friend uint128 operator + (const uint128& l, const int32_t& r) { return uint128(l) += uint128(r); }
  40. friend uint128 operator + (const uint64_t& l, const uint128& r) { return uint128(l) += r; }
  41. friend uint128 operator - (const uint128& l, const uint128& r) { return uint128(l) -= r; }
  42. friend uint128 operator * (const uint128& l, const uint128& r) { return uint128(l) *= r; }
  43. friend uint128 operator * (const uint128& l, const uint64_t& r) { return uint128(l) *= uint128(r); }
  44. friend uint128 operator * (const uint128& l, const uint32_t& r) { return uint128(l) *= uint128(r); }
  45. friend uint128 operator | (const uint128& l, const uint128& r) { return uint128(l) = (r); }
  46. friend uint128 operator & (const uint128& l, const uint128& r) { return uint128(l) &= r; }
  47. friend uint128 operator & (const uint128& l, const uint64_t& r) { return uint128(l) &= uint128(r); }
  48. friend uint128 operator ^ (const uint128& l, const uint128& r) { return uint128(l) ^= r; }
  49. friend uint128 operator << (const uint128& l, const uint128& r) { return uint128(l) <<= r; }
  50. friend uint128 operator >> (const uint128& l, const uint128& r) { return uint128(l) >>= r; }
  51. friend uint128 operator >> (const uint128& l, const int32_t& r) { return uint128(l) >>= uint128(r); }
  52. friend bool operator > (const uint128& l, const uint128& r) { return r < l; }
  53. friend bool operator >(const uint128& l, const int64_t& r) { return uint128(r) < l; }
  54. friend bool operator > (const int64_t& l, const uint128& r) { return r < uint128(l); }
  55.  
  56. friend bool operator >= (const uint128& l, const uint128& r) { return l == r || l > r; }
  57. friend bool operator >= (const uint128& l, const int64_t& r) { return l >= uint128(r); }
  58. friend bool operator >= (const int64_t& l, const uint128& r) { return uint128(l) >= r; }
  59. friend bool operator <= (const uint128& l, const uint128& r) { return l == r || l < r; }
  60. friend bool operator <= (const uint128& l, const int64_t& r) { return l <= uint128(r); }
  61. friend bool operator <= (const int64_t& l, const uint128& r) { return uint128(l) <= r; }
  62.  
  63. operator uint64_t() { return lo; } //强制转换为uint64_t
  64. operator uint32_t() { return (uint32_t)lo; } //强制转换为uint32_t
  65. operator int32_t() { return (int32_t)lo; } //强制转换为int32_t
  66.  
  67. uint32_t low_32_bits()const { return (uint32_t)lo; }
  68. uint64_t low_bits()const { return lo; }
  69. uint64_t high_bits()const { return hi; }
  70.  
  71. uint64_t hi;
  72. uint64_t lo;
  73. };

实现函数:

  1. uint128::uint128(const std::string &sz)
  2. :hi(), lo()
  3. {
  4. // do we have at least one character?
  5. if (!sz.empty()) {
  6. // make some reasonable assumptions
  7. int radix = ;
  8. bool minus = false;
  9.  
  10. std::string::const_iterator i = sz.begin();
  11.  
  12. // check for minus sign, i suppose technically this should only apply
  13. // to base 10, but who says that -0x1 should be invalid?
  14. if (*i == '-') {
  15. ++i;
  16. minus = true;
  17. }
  18.  
  19. // check if there is radix changing prefix (0 or 0x)
  20. if (i != sz.end()) {
  21. if (*i == '') {
  22. radix = ;
  23. ++i;
  24. if (i != sz.end()) {
  25. if (*i == 'x') {
  26. radix = ;
  27. ++i;
  28. }
  29. }
  30. }
  31.  
  32. while (i != sz.end()) {
  33. unsigned int n = ;
  34. const char ch = *i;
  35.  
  36. if (ch >= 'A' && ch <= 'Z') {
  37. if (((ch - 'A') + ) < radix) {
  38. n = (ch - 'A') + ;
  39. }
  40. else {
  41. break;
  42. }
  43. }
  44. else if (ch >= 'a' && ch <= 'z') {
  45. if (((ch - 'a') + ) < radix) {
  46. n = (ch - 'a') + ;
  47. }
  48. else {
  49. break;
  50. }
  51. }
  52. else if (ch >= '' && ch <= '') {
  53. if ((ch - '') < radix) {
  54. n = (ch - '');
  55. }
  56. else {
  57. break;
  58. }
  59. }
  60. else {
  61. /* completely invalid character */
  62. break;
  63. }
  64.  
  65. (*this) *= radix;
  66. (*this) += n;
  67.  
  68. ++i;
  69. }
  70. }
  71.  
  72. if (minus) {
  73. *this = -*this;
  74. }
  75. }
  76. }
  77.  
  78. uint128& uint128::operator<<=(const uint128& rhs)
  79. {
  80. if (rhs >= )
  81. {
  82. hi = ;
  83. lo = ;
  84. }
  85. else
  86. {
  87. unsigned int n = rhs.to_integer();
  88. const unsigned int halfsize = / ;
  89.  
  90. if (n >= halfsize){
  91. n -= halfsize;
  92. hi = lo;
  93. lo = ;
  94. }
  95.  
  96. if (n != ) {
  97. // shift high half
  98. hi <<= n;
  99.  
  100. const uint64_t mask(~(uint64_t(-) >> n));
  101.  
  102. // and add them to high half
  103. hi |= (lo & mask) >> (halfsize - n);
  104.  
  105. // and finally shift also low half
  106. lo <<= n;
  107. }
  108. }
  109.  
  110. return *this;
  111. }
  112.  
  113. uint128 & uint128::operator>>=(const uint128& rhs)
  114. {
  115. if (rhs >= )
  116. {
  117. hi = ;
  118. lo = ;
  119. }
  120. else
  121. {
  122. unsigned int n = rhs.to_integer();
  123. const unsigned int halfsize = / ;
  124.  
  125. if (n >= halfsize) {
  126. n -= halfsize;
  127. lo = hi;
  128. hi = ;
  129. }
  130.  
  131. if (n != ) {
  132. // shift low half
  133. lo >>= n;
  134.  
  135. // get lower N bits of high half
  136. const uint64_t mask(~(uint64_t(-) << n));
  137.  
  138. // and add them to low qword
  139. lo |= (hi & mask) << (halfsize - n);
  140.  
  141. // and finally shift also high half
  142. hi >>= n;
  143. }
  144. }
  145. return *this;
  146. }
  147.  
  148. uint128& uint128::operator*=(const uint128 &b)
  149. {
  150. uint64_t a0 = (uint32_t)(this->lo);
  151. uint64_t a1 = (uint32_t)(this->lo >> 0x20);
  152. uint64_t a2 = (uint32_t)(this->hi);
  153. uint64_t a3 = (uint32_t)(this->hi >> 0x20);
  154.  
  155. uint64_t b0 = (uint32_t)(b.lo);
  156. uint64_t b1 = (uint32_t)(b.lo >> 0x20);
  157. uint64_t b2 = (uint32_t)(b.hi);
  158. uint64_t b3 = (uint32_t)(b.hi >> 0x20);
  159.  
  160. this->hi = ;
  161. this->lo = a3*b0;
  162. (*this) += a2*b1;
  163. (*this) += a1*b2;
  164. (*this) += a0*b3;
  165. (*this) <<= 0x20;
  166. (*this) += a2*b0;
  167. (*this) += a1*b1;
  168. (*this) += a0*b2;
  169. (*this) <<= 0x20;
  170. (*this) += a1*b0;
  171. (*this) += a0*b1;
  172. (*this) <<= 0x20;
  173. (*this) += a0*b0;
  174.  
  175. return *this;
  176. }

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. IDEA Java 源发行版 8 需要目标发行版 1.8

     [问题记录] maven新建的一个项目,需要到一些java8的一些特性,但是在编译的时候就报错了,提示这样的错误. 我是在用二进制字面量出现的这个问题,二进制自变量是Java7的特性, 你可以这样写 ...

  2. 【AtCoder】CODE FESTIVAL 2016 qual A

    CODE FESTIVAL 2016 qual A A - CODEFESTIVAL 2016 -- #include <bits/stdc++.h> #define fi first # ...

  3. Kubernetes---启动及退出动作

    apiVersion: v1 kind: Pod metadata: name: lifecycle-demo spec: containers: - name:lifecycle-demo-cont ...

  4. C/C++快读(快速读入)有多——安全AC

    在一些算法题目中中,有的程序会被卡常(数),就是说,程序虽然渐进复杂度,(通俗来讲:算法的时间复杂度)可以接受,但因为算法本身的时间常数过大,导致程序在一些算法竞赛中超时.这是,快读就显得尤为重要了. ...

  5. centos8自定义目录安装php7.3

    1.目录结构 源码目录:/home/werben/pkgsrc/php-7.3.11 安装目录:/home/werben/application/php7.3.11 2.下载php源码 # 官网地址: ...

  6. S02_CH01_Hello World实验

    S02_CH01_Hello World实验 ZYNQ是一款SOC芯片,在前面第一季的学习当中,我们只是粗略的学习了ZYNQ的PL部分,对于ZYNQ最突出的功能,其内部的双核Cortex-A9内核并未 ...

  7. .Net下二进制形式的文件存储与读取

    .Net下图片的常见存储与读取凡是有以下几种:存储图片:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[].1.参数是图片路径:返回 ...

  8. SpringBoot + Dubbo + zookeeper 搭建简单分布式服务

    SpringBoot + Dubbo + zookeeper 搭建简单分布式服务 详细操作及源码见: https://github.com/BillyYangOne/dubbo-springboot

  9. Vue.prototype详解

    参考地址:Vue.prototype详解 如果需要设置 全局变量,在main.js中,Vue实例化的代码里添加. 不想污染全局作用域.这种情况下,你可以通过在 原型 上定义它们使其在每个Vue实例中可 ...

  10. STM32点亮LED

    原理图 测试灯,接GPIO外设B,Pin 12 举例 前提,工程模版建立好 #include "stm32f10x.h" void delay(u32 i) { while(i-- ...