4

From following code I expect to set all bits in x to 1, but somehow only first 32 bits are set:

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.

Thx

4

3 回答 3

13

x<<32计算x左移 32 位的结果,对 value 不做任何事情。你想x <<= 32改用。

于 2013-05-02T23:16:11.817 回答
10

为什么不int64_t x = -1呢?还是uint64_t x = ~0

于 2013-05-02T23:16:36.897 回答
4

这将起作用:

int64_t x = ~0LL;   (iner

或者

int64_t x = -1LL;

您可能会逃脱没有LL,但不能保证 - 取决于编译器。

于 2013-05-02T23:17:11.790 回答