分区不跨越字节

cal*_*pto 6 c++ math biginteger integer-division

我试图对uint128_t由2 uint64_t秒组成的分区进行分区.奇怪的是,该函数适用于uint64_ts,只有较低的值集和较高的值= 0.我不明白为什么.

这是除法和位移的代码

class uint128_t{
   private:
      uint64_t UPPER, LOWER;
   public:
      // lots of stuff

    uint128_t operator<<(int shift){
        uint128_t out;
        if (shift >= 128)
            out = uint128_t(0, 0);
        else if ((128 > shift) && (shift >= 64))
            out = uint128_t(LOWER << (64 - shift), 0);
        else if (shift < 64)
            out = uint128_t((UPPER << shift) + (LOWER >> (64 - shift)), LOWER << shift);
        return out;
    }

    uint128_t operator<<=(int shift){
        *this = *this << shift;
        return *this;
    }

    uint128_t operator/(uint128_t rhs){
            // copy of numerator = copyn
            uint128_t copyn(*this), quotient = 0;// constructor: uint128_t(T), uint128_t(S, T), uint128_t(uint128_t), etc
            while (copyn >= rhs){
                // copy of denomiator = copyd
                // temp is the current quotient bit being worked with
                uint128_t copyd(rhs), temp(1);
                // shift the divosr to the highest bit
                while (copyn > (copyd << 1)){
                    copyd <<= 1;
                    temp <<= 1;
                }
                copyn -= copyd;
                quotient += temp;
            }
            return quotient;
        }
// more stuff
};
Run Code Online (Sandbox Code Playgroud)

请忽略我公然无视内存管理.

Mar*_*k B 3

out = uint128_t(LOWER << (64 - shift), 0);是错误的——应该是shift - 64

作为风格注释,ALL_CAPITALS 通常仅保留给常量。变量和成员应大部分使用小写。

  • 实际上,全部大写通常是为预处理器标识符保留的。 (2认同)