Understanding the warning "comparing advanced ~ unsigned to unsigned"

I came across a warning that I really don't understand. A warning is generated by comparing what I consider unsigned with another unsigned.

Here is the source:

#include <stdint.h> #include <stdlib.h> #include <stdio.h> #include <assert.h> 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; } 

What I create using the following procedure:

 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] 

I am using gcc version 4.7.2 20121109 (Red Hat 4.7.2-8)

How can I fix the above comparison?

+4
source share
2 answers

I had the same problem and worked on it using an intermediate variable:

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

Source: https://habr.com/ru/post/1498977/


All Articles