我很擅长处理比特,并且在编译时遇到以下警告:
Run Code Online (Sandbox Code Playgroud)7: warning: left shift count >= width of type
我的第7行看起来像这样
unsigned long int x = 1 << 32;
Run Code Online (Sandbox Code Playgroud)
如果long我的系统的大小是32位,这将是有意义的.但是,sizeof(long)返回8并CHAR_BIT定义为8建议long应为8x8 = 64位长.
我在这里错过了什么?是sizeof和CHAR_BIT不准确还是我误解了一些基本的东西?
让我们考虑以下计划test.c:
#include <stdio.h>
struct test {
unsigned int a:5;
};
int main () {
unsigned int i;
struct test t = {1};
for (i = 0; i < t.a << 1; i++)
printf("%u\n", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译时gcc -Wsign-compare test.c生成以下警告(使用gcc 4.8.1测试):
test.c:9:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < t.a << 1; i++)
^
Run Code Online (Sandbox Code Playgroud)
clang -Wsign-compare test.c 产生以下内容(使用clang 3.2进行测试):
test.c:9:19: warning: comparison of integers of different signs: 'unsigned …Run Code Online (Sandbox Code Playgroud)