理解警告“提升〜无符号与无符号的比较”

我遇到了一个我不太懂的警告。 通过比较我认为是无符号的和未签名的符号来生成警告。

这是来源:

#include  #include  #include  #include  int main() { uint8_t *arr = malloc(8); assert(arr); /* fill arr[] with stuff */ if (arr[3] != (uint8_t)(~arr[2])) { // Warning is here /* stuff */ } return EXIT_SUCCESS; } 

我使用以下过程构建的:

 user@linux:~ $ gcc -o test -Wall -Wextra test.c test.c: In function 'main': test.c:13:16: warning: comparison of promoted ~unsigned with unsigned [-Wsign-compare] 

我正在使用gcc版本4.7.2 20121109(Red Hat 4.7.2-8)

我该如何解决上述比较?

是的我认为这是一个错误。 阅读[错误c / 38341]提醒〜无符号与无符号的错误警告比较以及为什么,警告:有符号和无符号整数表达式之间的比较? 。 这可能对你有帮助。

我有同样的问题,我通过使用一个中间变量来解决它:

 uint8_t check = ~arr[2]; if (arr[3] != check)