This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

enum, out of range

Hello,

I have added U postfix to the integer but the problem still is the same.

I read following pages but didn't work.
http://www.keil.com/support/docs/3243.htm
http://www.keil.com/forum/14703/

#define M(x)         (u32)(1U << (x%32))

enum{
   eFirst = M(x)
   .....
   eLast = M(31)     // generate "integer operation result is out of range"
}

Can I avoid this warning?

Parents
  • If assuming a 32-bit machine with two-complement integers, the value

    (int)(1u << 31)
    


    would be possible to store in an enum for the C language.

    But the actual value would be negative. So you would then need to continue to care about type casts to get your enumerated values to fold back into unsigned values again.

    So it's better to let the enum just hold the bit positions (0..31) and in the actual code write (1u << bitname). The compiler will still see that both 1u and bitname are constants, allowing it to pre-compute the shifted value if the processor doesn't have a suitable instruction to do the shift on-the-fly.

Reply
  • If assuming a 32-bit machine with two-complement integers, the value

    (int)(1u << 31)
    


    would be possible to store in an enum for the C language.

    But the actual value would be negative. So you would then need to continue to care about type casts to get your enumerated values to fold back into unsigned values again.

    So it's better to let the enum just hold the bit positions (0..31) and in the actual code write (1u << bitname). The compiler will still see that both 1u and bitname are constants, allowing it to pre-compute the shifted value if the processor doesn't have a suitable instruction to do the shift on-the-fly.

Children
No data