From following code I expect to set all bits in x to 1, but somehow only first 32 bits are set:
从下面的代码我希望将x中的所有位设置为1,但不知何故只设置了前32位:
int64_t x = 0xFFFFFFFF;
x<<32;
x|=0xFFFFFFFF;
Note: printing x after each line results in 4294967295 (32 lower bits set to 1). Also, tried using numeric_limits<int64_t>::min()
with no success. My question is how to set all bits in x? Using RHEL5.5.
注意:在每行之后打印x会产生4294967295(32位低位设置为1)。此外,尝试使用numeric_limits
Thx
3 个解决方案
#1
13
x<<32
calculates the result of shifting x
left by 32 bits and does nothing with the value. You want to use x <<= 32
instead.
x << 32计算将x向左移位32位的结果,并且不对该值进行任何操作。你想用x << = 32代替。
#2
10
Why not int64_t x = -1
? or uint64_t x = ~0
?
为什么不int64_t x = -1?或uint64_t x = ~0?
#3
4
This will work:
这将有效:
int64_t x = ~0LL; (iner
or
int64_t x = -1LL;
You may get away with not having the LL
, but not guaranteed - depends on the compiler.
你可能没有LL,但没有保证 - 取决于编译器。
#1
13
x<<32
calculates the result of shifting x
left by 32 bits and does nothing with the value. You want to use x <<= 32
instead.
x << 32计算将x向左移位32位的结果,并且不对该值进行任何操作。你想用x << = 32代替。
#2
10
Why not int64_t x = -1
? or uint64_t x = ~0
?
为什么不int64_t x = -1?或uint64_t x = ~0?
#3
4
This will work:
这将有效:
int64_t x = ~0LL; (iner
or
int64_t x = -1LL;
You may get away with not having the LL
, but not guaranteed - depends on the compiler.
你可能没有LL,但没有保证 - 取决于编译器。