INT128
Posted dgklr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了INT128相关的知识,希望对你有一定的参考价值。
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << " " << "Line: " << __LINE__ << " :"
using namespace std;
class INT128{
public:
#define OPERATOR 9223372036854775808ull //(1 << 63)
#define HERE (*this)
unsigned long long HighPart, LowPart;
INT128(){ HighPart = 0; LowPart = 0; }
INT128(long long p)
{ HighPart = 0; LowPart = 0; if (p >= 0) LowPart = p; else LowPart = p, HERE.rev(); }
INT128(long long p,long long q){ HighPart = p; LowPart = q; }
void print(){ cout << (bitset<64>)HighPart << ' ' << (bitset<64>)LowPart; }
void printH(){ cout << hex << HighPart << LowPart << dec << endl; }
INT128 operator | (INT128 b) {return ((HighPart | b.HighPart), (LowPart | b.LowPart));}
INT128 operator & (INT128 b) {return ((HighPart & b.HighPart), (LowPart & b.LowPart));}
INT128 operator ^ (INT128 b) {return ((HighPart ^ b.HighPart), (LowPart ^ b.LowPart));}
bool operator < (INT128 b) {return (HERE - b).HighPart & OPERATOR;}
bool operator <= (INT128 b) {return (HERE == b) | (HERE < b);}
bool operator == (INT128 b) {return (HighPart == b.HighPart) && (LowPart == b.LowPart);}
#define QAdd(a,b) INT128(a.HighPart + b.HighPart + (a.LowPart >= -b.LowPart && (b.LowPart != 0)), a.LowPart + b.LowPart) // what the fuck?
inline INT128 rev(){
INT128 p = HERE;
p.HighPart = ~p.HighPart; p.LowPart = ~p.LowPart;
return QAdd(p,((INT128)1));}
INT128 operator << (int st){ if (st == 0) return HERE;
if (st >= 64) return INT128(LowPart << (st - 64),0);
else return INT128((HighPart << st | LowPart >> (64 - st)),LowPart << st);}
INT128 operator >> (int st){ if (st == 0) return HERE;
if (st >= 64) return INT128(0, HighPart >> (st - 64));
else return INT128(HighPart >> st,(LowPart >> st | HighPart << (64 - st)));}
bool operator [] (const int st){
return (st >= 64 ? ((HighPart >> (st - 64)) & 1) : ((LowPart >> (st)) & 1));}
void change(int x, bool p){ // build successfully
if (p) if (x >= 64) HighPart |= (1ll << (x - 64));
else LowPart |= (1ll << (x));
else if (x >= 64)
if (((HighPart >> (x - 64)) & 1)) HighPart ^= (1ll << (x - 64)); else;
else if (((LowPart >> (x)) & 1)) LowPart ^= (1ll << x);
}
INT128 operator / (long long st){ // build successfully
INT128 nx = HERE;
bool flag = (st & OPERATOR) ^ (HighPart & OPERATOR);
if (st & OPERATOR) st = -st;
if (HighPart & OPERATOR) nx = -nx;
INT128 tmp = st; assert(st != 0);
int len = log2(st)+1; INT128 ret = 0;
for (int i=127-len;i>=0;i--){
if ((tmp << i) <= nx) ret.change(i, 1), nx = nx - (tmp << i);
nx.print(); cout << endl;
}
if (flag) return -ret; else return ret;
}
long long operator % (long long st){
long long ret = 0;
for (int i=127;i>=0;i--){
ret = ret * 2 + HERE[i];
if (ret >= st) ret -= st;
}
return ret;
}
friend ostream& operator << (ostream &st, INT128 t){
static int a[51]; int top = 0;
if (t < 0) putchar('-'), t = -t;
while (t.HighPart || t.LowPart){
a[top++] = t % 10;
t = t / 10;
t.print(); cout << endl;
}
while (top) putchar(a[--top] + '0');
return st;
}
INT128 operator + (INT128 p){ return QAdd(HERE,p); }
INT128 operator - (INT128 p){ return QAdd(HERE,p.rev()); }
INT128 operator - (){ return HERE.rev(); }
const INT128& operator=(const long long& p){ HERE = INT128 (p); return HERE; }
// operator long long(){ return LowPart; }
#undef OPERATOR
#undef HERE
}; // IF you just want to add, PLEASE use QAdd()
以上是关于INT128的主要内容,如果未能解决你的问题,请参考以下文章
如何将这个 Objective-C 代码片段写入 Swift?
java.lang.NullPointerException: Attempt to invoke virtual method ‘int android.database.sqlite异常(代码片段