码迷,mamicode.com
首页 > 其他好文 > 详细

INT128

时间:2020-02-02 23:20:13      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:deb   please   set   func   c++   space   signed   its   flag   

#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
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

标签:deb   please   set   func   c++   space   signed   its   flag   

原文地址:https://www.cnblogs.com/dgklr/p/12254049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!