C左移64位失败

时间:2022-09-01 10:22:32

I have this code in C (it's for study only):

我在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);        
    }       

What is printed:

什么是印刷品:

20 - 2621448
21 - 5505032
22 - 11534344
23 - 24117256
24 - 50331656
25 - 104857608
26 - 218103816
27 - 18446744073625665544
28 - 18446744073575333896
29 - 18446744073508225032

Why at x > 26 do I have those strange values? I'm at gcc 4.6.1 on Ubuntu 10.10 64 bits.

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

2 个解决方案

#1


19  

Because 1 is an int, 32 bits, so (1 << 27)*27 overflows. Use 1ull.

因为1是int,32位,所以(1 << 27)* 27溢出。使用1ull。

Regarding your comment, if x is a uint64_t, then 1 << x is still an int, but for the multiplication it would be cast to uint64_t, so there'd be no overflow. However, if x >= 31, 1 << x would be undefined behaviour (as the resulting value cannot be represented by a signed 32 bit integer type).

关于你的注释,如果x是uint64_t,那么1 << x仍然是一个int,但是对于乘法它将被转换为uint64_t,所以没有溢出。但是,如果x> = 31,1 << x将是未定义的行为(因为结果值不能用带符号的32位整数类型表示)。

#2


0  

I guess your problem is, you calculate with 32bit and assign it later to a 64 bit value

我猜您的问题是,您使用32位计算并稍后将其分配给64位值

division by 64 is the same as not shift 6 bit

除以64与64位相同

char x;
uint64_t one = 1;
uint64_t total = 0;

for(x = 20; x < 30; x++){
    total = ((((one << (x - 6)) * x) + 1) * sizeof(uint64_t));
    printf("%d - %llu\n", x, total);        
}

not compiled yet

尚未编译

#1


19  

Because 1 is an int, 32 bits, so (1 << 27)*27 overflows. Use 1ull.

因为1是int,32位,所以(1 << 27)* 27溢出。使用1ull。

Regarding your comment, if x is a uint64_t, then 1 << x is still an int, but for the multiplication it would be cast to uint64_t, so there'd be no overflow. However, if x >= 31, 1 << x would be undefined behaviour (as the resulting value cannot be represented by a signed 32 bit integer type).

关于你的注释,如果x是uint64_t,那么1 << x仍然是一个int,但是对于乘法它将被转换为uint64_t,所以没有溢出。但是,如果x> = 31,1 << x将是未定义的行为(因为结果值不能用带符号的32位整数类型表示)。

#2


0  

I guess your problem is, you calculate with 32bit and assign it later to a 64 bit value

我猜您的问题是,您使用32位计算并稍后将其分配给64位值

division by 64 is the same as not shift 6 bit

除以64与64位相同

char x;
uint64_t one = 1;
uint64_t total = 0;

for(x = 20; x < 30; x++){
    total = ((((one << (x - 6)) * x) + 1) * sizeof(uint64_t));
    printf("%d - %llu\n", x, total);        
}

not compiled yet

尚未编译