相关疑难解决方法(0)

C左移64位失败

我在C中有这个代码(仅供学习):

    char x;
    uint64_t total = 0;

    for(x = 20; x < 30; x++){
        total = (((((1 << x) * x) / 64) + 1) * sizeof(uint64_t));
        printf("%d - %llu\n", x, total);        
    }       
Run Code Online (Sandbox Code Playgroud)

什么是印刷品:

20 - 2621448
21 - 5505032
22 - 11534344
23 - 24117256
24 - 50331656
25 - 104857608
26 - 218103816
27 - 18446744073625665544
28 - 18446744073575333896
29 - 18446744073508225032
Run Code Online (Sandbox Code Playgroud)

为什么在x> 26时我有那些奇怪的值?我在Ubuntu 10.10 64位上的gcc 4.6.1.

c 64-bit bit-shift

8
推荐指数
1
解决办法
6992
查看次数

为什么没有留下位移<<在long int数据类型中移位超过31?

我想在我的程序中使用以下代码,但是gcc不允许我将我的1转移到31以上.

sizeof(long int) 显示8,所以这并不意味着我可以离开直到63?

#include <iostream>

using namespace std;

int main(){
    long int x;
    x=(~0 & ~(1<<63));
    cout<<x<<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译输出以下警告:

left shift `count >= width` of type [enabled by default] `x=(~0 & ~(1<<63))`;
                                                                    ^
Run Code Online (Sandbox Code Playgroud)

输出为-1.如果我离开31位,我得到2147483647正如预期的那样.

我希望除了MSB之外的所有位都打开,从而显示数据类型可以容纳的最大值.

c++ bit-shift

8
推荐指数
1
解决办法
6599
查看次数

标签 统计

bit-shift ×2

64-bit ×1

c ×1

c++ ×1