The compiler warns you that the type of the ternary expression is int
, a signed type, whereas the type of the assigned object is uint8_t
, an unsigned type. Mixing signed and unsigned types in expressions can lead to counter intuitive behavior, such as sizeof(int) > -1
being false.
It could also warn you that storing an int
value into a unt8_t
variable may cause an implicit conversion that would change the value. But in this particular case, range analysis can easily prove that any possible outcome of this expression is in the range of type uint8_t
so none of the above cases require a warning. This compiler is obnoxious and not smart enough.
The warning can be silenced with an extra cast:
uint8_t color = (uint8_t)((bitmap & mask) ? fg : bg);
but such useless casts obfuscate the code and confuse the reader. Cast should be avoided in most cases as they can lead to spurious bugs.
Your best solution seems to keep the if
statement. The compiler might actually generate faster code for this solution as it is a good candidate for a conditional store instruction, producing efficient branchless code.