#include <stdio.h>
#include <stdint.h>
int main(){
uint64_t a = 1 << 63;
/* do some thing */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
$ gcc -Wall -Wextra -std=c99 test.c -o test
warning: left shift count >= width of type [-Wshift-count-overflow]
Run Code Online (Sandbox Code Playgroud)
问:uint64_t应该有64位宽,为什么左移操作溢出?
1是int您的平台上只有32位,或者它可能是64位但签名.
用于首先(uint64_t)1 << 63转换1为64位无符号整数.(或者((uint64_t)1) << 63如果你愿意的话)