On 7/18/2012 4:56 AM, Martin Kojtal wrote:
> Hello,
>
> the example application is running on 8bit MCU, register is 8 bit wide. There's line where it has to clear the flag with following command:
>
> StructPointer->REGISTER &= ~(FIRST_BIT_MASK);
>
> It issues warning "possible loss of data" which I understand because ~ operator have operands integer type (6.5.4 Expressions). Why the warning disappears when the bit mask is casted to UINT8:
>
> StructPointer->REGISTER &= ~((UINT
FIRST_BIT_MASK);
>
> I am inclined to cast the result of bitwise complement: (UINT
(~FIRST_BIT_MASK).
> Are both forms correct to suppress the warning?
The compiler can issue warnings for any reason, even for C code
that is perfectly valid. Sometimes you'll be able to silence a
warning by changing one valid fragment to another, sometimes not.
(Sometimes, a rewrite that placates one compiler will upset another!)
In your case it looks like the (UINT

cast silences your compiler's
warning -- but "Why?" is a question about the compiler, not about C.
We can't tell whether your two versions are equivalent, because
we don't know the data types of FIRST_BIT_MASK and REGISTER (we
don't actually *know* what UINT8 is, either, though we might guess).
Under reasonable assumptions about the data types and about the way
your machine probably represents negative integers, the two versions
do the same thing -- so the compiler's nervousness about one spelling
and acceptance of the other is not a C thing, but a compiler thing.
Perhaps the compiler's documentation says something about the matter.
--
Eric Sosman
d