How do you explain that line 7 gets a warning, but not line 5 or line 6?
int main()
{
unsigned char a = 0xFF;
unsig
Compilers are built by people and they don't have infinite time to figure out all arithmetic possibilities to decide, which cases are worth issuing a warning.
So I believe (attention opinion) that compiler engineers would go the following way:
I would expect people to write code where either the result is casted to (unsigned char)
or where the outermost operator masks all higher bytes with a constant.
a = (unsigned char) ( /* some obscure bit-wise expressoin */ );
would be OK thena = 0xff & ( /* some obscure bit-wise expressoin */ );
also OKif you know that your compiler translates those two patterns correctly the other cases shouldn't bother you too much.
I've seen compilers that would issue a warning because of a = a | b;
so GCC not giving a warning is a free bonus. it might be, that gcc just infers the constant assignment in a | b
and therefore replaces it with 0xff | 0xff
which is known to work without problems. If that happens though I don't know why it cannot derive the constant value of a
in the other statements.