如何在C中使用uint64_t
#include #include int main(){ uint64_t a = 1 << 63; /* do some thing */ return 0; }
$ gcc -Wall -Wextra -std=c99 test.c -o test warning: left shift count >= width of type [-Wshift-count-overflow]
问: uint64_t
应该有64位宽,为什么左移操作会溢出?
1
是一个int
,它在您的平台上只有32位,或者它可以是64位但是已签名。
使用(uint64_t)1 << 63
转换1
到64位无符号整数。 (或者((uint64_t)1) << 63
如果你愿意的话)