操作符>>和<<输出在输出时为空。
|
我不明白为什么当我输出它们时两个函数的输出都为空:
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
谁能弄清楚为什么?
没有找到相关结果
已邀请:
3 个回复
徐百晴墓斜
,
(最高半字节)是
(二进制),没有设置高位。所以0是正确的输出。 如果将
更改为
(或大于0x7的任何值),则很可能在输出中看到0 ... 1。
荆怖赡
。
是因为
与右值
不匹配,而
匹配
时也匹配
将选择模板分配运算符,这将仅分配较低的uint64_t。您应该将第一个分配的操作员的签名更改为
弓萍功
的类型是64位整数。另外,您尝试将整数移位63位,而第64位在您的情况下为零。因此,您会丢失所有实际持有该数字的63位。 PS:您说过不喜欢使用调试器,但是如果只使用一个调试器,那么真的很容易自己搞清楚。