小编boo*_*leg的帖子

无符号整数位域移位产生有符号整数

让我们考虑以下计划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)

c gcc bit-shift clang bit-fields

12
推荐指数
1
解决办法
1138
查看次数

标签 统计

bit-fields ×1

bit-shift ×1

c ×1

clang ×1

gcc ×1