操作符>>和<<输出在输出时为空。

| 我不明白为什么当我输出它们时两个函数的输出都为空:
class uint128_t{

private:
    uint64_t UPPER, LOWER;

public:
    // constructors
    uint128_t(){
        UPPER = 0;
        LOWER = 0;
    }

    template <typename T>
    uint128_t(T rhs){
        UPPER = 0;
        LOWER = (uint64_t) rhs;
    }

    template <typename S, typename T>
    uint128_t(const S upper_rhs, const T lower_rhs){
        UPPER = (uint64_t) upper_rhs;
        LOWER = (uint64_t) lower_rhs;
    }

    uint128_t(const uint128_t & rhs){
        UPPER = rhs.UPPER;
        LOWER = rhs.LOWER;
    }

    //  RHS input args only

    // assignment operator
    template <typename T> uint128_t & operator=(T rhs){
        UPPER = 0;
        LOWER = (uint64_t) rhs;
        return *this;
    }

    uint128_t & operator=(uint128_t & rhs){
        UPPER = rhs.UPPER;
        LOWER = rhs.LOWER;
        return *this;
    }


    uint128_t operator<<(int shift){
        if (shift >= 128)
            return uint128_t(0, 0);
        else if (shift == 64)
            return uint128_t(LOWER, 0);
        else if (shift < 64)
            return uint128_t((UPPER << shift) + (LOWER >> (64 - shift)), LOWER << shift);
        else if ((128 > shift) && (shift > 64)){
            uint128_t a =uint128_t(LOWER << (shift - 64), 0);
            // a will show the correct values 
            std::cout << a.upper() << \" \" << a.lower() << std::endl;
            return uint128_t(LOWER << (shift - 64), 0);
            // in the program that includes this, printing out the values show 0 0
        }
    }

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

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

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

    const uint64_t upper() const {
        return UPPER;
    }

    const uint64_t lower() const {
        return LOWER;
    }

// lots of other stuff

};

int main(){

    uint128_t a(0x123456789abcdef1ULL, 0x123456789abcdef1ULL);

    a>>= 127; // or a <<= 127;
    std::cout <<a.upper() << \" \" <<a.lower() << std::endl;
    return 0;
}
http://ideone.com/jnZI9 谁能弄清楚为什么?     
已邀请:
        您的int为128位,您将其向下(向右)移位127位,这意味着最高位将移至最低位,所有其他位将为0。 但是在您的示例中,int是
0x1....
0x1
(最高半字节)是
0001
(二进制),没有设置高位。所以0是正确的输出。 如果将
0x1...
更改为
0x8...
(或大于0x7的任何值),则很可能在输出中看到0 ... 1。     
        “ 6”表示移出数字中最右边的127位。由于您的uint128_t为0x1234 ....,因此最高有效位为\'0 \'。在ѭ7之后,数字变为0,因此输出如预期的那样是
0 0
<<=
是因为
uint128_t & operator=(uint128_t & rhs)
与右值
rhs
不匹配,而
template <typename T> uint128_t & operator=(T rhs)
匹配
T == uint128_t
时也匹配
    *this = *this << shift;
将选择模板分配运算符,这将仅分配较低的uint64_t。您应该将第一个分配的操作员的签名更改为
uint128_t& operator=(const uint128_t& rhs)
//                   ^^^^^
    
        原因如下:
UPPER
的类型是64位整数。另外,您尝试将整数移位63位,而第64位在您的情况下为零。因此,您会丢失所有实际持有该数字的63位。 PS:您说过不喜欢使用调试器,但是如果只使用一个调试器,那么真的很容易自己搞清楚。     

要回复问题请先登录注册