The problem is that char
in the C and C ++ standards determines that it can be either a signed or unsigned value, and must contain at least 8 bits. It appears that the architecture and the compiler use signed 8-bit char
values.
This means that any value with the highest bit (bit 7) will be negative. Thus, 0x80 as char
becomes -128 decimal.
The constant 0x80 is not a char
value, it is an int
value.
So, when you compare -128 with 0x80 (128), they do not match and can never be, and the compiler understands this and issues a warning.
There are many ways to achieve this, here are a few possible scenarios:
First, we can use any value for another:
if (((int)byte) & 0xff == 0x80)
or
if (byte == (char)0x80)
Alternatively, we can make the constant a char
value, not an int
value.
if (byte == '\200')
or
if (byte == '\x80')
Alternatively use unsigned char byte = 0x80;
- indicating that it is unsigned char guarantees that it does not flip to negative.
source share