Search code examples
compiler-warningsbitwise-operatorsones-complement

warning : 'integer conversion results in truncation'


I get an warning here. The warning says 'integer conversion results in truncation'. It persists even if I remove the typecast(U16).

typedef unsigned short  U16;
U16 mask;
mask = ~(U16)(0x8000);

How do I resolve this warning? I used the below code and removed the warning, but unsure if its the right way to do it.

mask = (U16)(~(U32)(0x8000));

Thanks in advance!


Solution

  • C compilers don't like when you try to assign constant values into an L-value that's not big enough to hold them. I would guess that the compiler authors assume you know what value should be used since you're declaring a constant, therefore something must be wrong if you're potentially truncating its value. Here's a solution that will work, but may not be your ideal outcome:

    typedef unsigned short  U16;
    U16 mask;
    mask = 0x7fff; //~0x8000;