将枚举定义转换为无符号整数

标签 c casting enums unsigned-integer iar

根据这篇 SO 帖子:
What is the size of an enum in C?
枚举类型具有 signed int 类型。

我想将枚举定义从 signed int 转换为 unsigned int

例如,在我的平台上,unsigned int 是 32 位宽。 我想创建一个枚举:

typedef enum hardware_register_e
{
    REGISTER_STATUS_BIT = (1U << 31U)
} My_Register_Bits_t;

我的编译器提示上面的定义超出范围(这是针对 signed int)。

如何声明 unsigned int enum 值?

编辑 1:

  1. 最好不要扩展到 64 位(因为代码驻留在 在嵌入式系统中)。
  2. 由于技能限制,此项目不允许使用 C++。 :-(

编辑2:

  • 编译器是 IAR Embedded Workbench for ARM7。

最佳答案

According to this SO post: What is the size of an enum in C? enum types have signed int type.

事情是 enum 类型 可以int 类型,但它们不是 C 中的 int。在 gcc 1)enum类型默认为unsigned int

enum 常量是 intenum 类型是实现定义的。

在您的例子中,enum 常量是 int,但您给它的值不适合 int。您不能在 C 中使用 unsigned int enum 常量,因为 C 表示它们是 int


<子> 1) gcc 实现定义枚举类型文档:“通常,如果枚举中没有负值,则类型为无符号整数,否则为 int”,在 http://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit_002dfields-implementation.html

关于将枚举定义转换为无符号整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11314545/

相关文章:

c++ - '<function-style-cast >' : cannot convert from ' int' to 'CString'

c# - Enum 上的扩展方法 - 无法公开 Enum 本身的方法

c# - 可以在通用枚举类型上使用扩展方法吗? (不是枚举值)

c - 信息 C5012 : loop not parallelized due to reason '1008'

javascript - 将 javascript 正则表达式转换为 posix 正则表达式

arrays - 使用flutter从Firestore获取用户数组数据

C++ casted realloc 导致内存泄漏

C++ 枚举类 quint64 类型

c - libcurl:使用 WriteMemoryCallback 时 curl_easy_perform 上的段错误

c - 如何用转义序列替换单引号和双引号(对于传递给 strtok 的字符串)