c - Bitwise operation with (signed) enum value -
i using enumerator values flags:
typedef enum { = 0x00, b = 0x01u, // u has no influence, expected c = 0x02u, // u has no influence, expected ... } enum_name; volatile unsigned char* reg = someaddress; *reg |= b;
according misra-c:2004 bitwise operations shall not done signed type. unfortunately, compiler iar use signed int (or short or char) underlying type of enums, , option can find relates size, not signedness ("--enum-is-int").
according iar c/c++ development guide arm, pages 169 , 211, can define type of enum
s if enable iar language extensions (-e
command-line option, or project > options > c/c++ compiler > language > allow iar extensions in ide).
in particular, should define "sentinel" value, make sure compiler chooses correct type. prefers signed types, , uses smallest possible integer type, sentinel should largest positive integer corresponding unsigned integer type can describe. example,
typedef enum { /* ... */ enum_u8_sentinel = 255u } enum_u8; typedef enum { /* ... */ enum_u16_sentinel = 65535u } enum_u16; typedef enum { /* ... */ enum_u32_sentinel = 4294967295ul } enum_u32;
Comments
Post a Comment