xcode C语言枚举声明中的十六进制表达式0x80000000是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16160342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does hexadecimal expression 0x80000000 mean in enumeration declarations in C language
提问by George
I'm reading the code of an iPhone sample project (Xcode IDE, Apple LLVM compiler 4.2). In a header file of an external library (written in C) for that iPhone sample project, there're some events declared in enumeration type:
我正在阅读 iPhone 示例项目(Xcode IDE、Apple LLVM 编译器 4.2)的代码。在该 iPhone 示例项目的外部库(用 C 编写)的头文件中,有一些以枚举类型声明的事件:
typedef enum _Application_Events
{
EVENT_EXIT = 0x80000000,
EVENT_TOUCH,
EVENT_DRAG,
EVENT_RELEASE_TOUCH,
EVENT_ROTATE_0,
EVENT_ROTATE_90,
EVENT_ROTATE_180,
EVENT_ROTATE_270
} Application_Events;
I don't understand what kind of values are assigned to those events. Is 0x80000000
supposed to be a big positive integer (2147483648
), or negative zero, or a negative integer (-2147483648
)?
我不明白为这些事件分配了什么样的值。被0x80000000
认为是一个很大的正整数(2147483648
),或负零或负整数(-2147483648
)?
I inspected in Xcode debugger, with the compiler being Apple LLVM compiler 4.2, the EVENT_EXIT
equals (int) -2147483648
and the EVENT_RELEASE_TOUCH
equals (int) -2147483645
and so on.
我在 Xcode 调试器中进行了检查,编译器是 Apple LLVM 编译器 4.2、EVENT_EXIT
equals(int) -2147483648
和EVENT_RELEASE_TOUCH
equals(int) -2147483645
等等。
Apparently, they're treated in two's complementrepresentation. An related post can be found here.
显然,它们以二进制补码表示形式处理。可以在此处找到相关帖子。
But what I'm not sure about now are these:
但我现在不确定的是这些:
(1) The underlying data type for 0x80000000
always being int
or something else in other situations? Is this depended on compiler or platform?
(1) 底层数据类型为0x80000000
always beint
还是其他情况下的别的什么?这取决于编译器还是平台?
(2) If I assigned a hexadecimal value to a signed integer like this, is it always interpreted as the two's complement representation? Is this depended on compiler or platform? A related post can be found here. Another reference can be found here.
(2) 如果我像这样将一个十六进制值赋给一个有符号整数,它是否总是被解释为二进制补码表示?这取决于编译器还是平台?可以在此处找到相关帖子。可以在此处找到另一个参考。
Please share some ideas. Thank you all :D
请分享一些想法。谢谢大家:D
回答by PaulProgrammer
Like many things in C-like languages, an enumeration is just an integer. Setting the first value like this will cause the compiler to increment from there, guaranteeing that all enumeration values are less than 0. (as a signed integer by 2s compliment, the high bit being set will indicate a negative number)
像 C 类语言中的许多东西一样,枚举只是一个整数。像这样设置第一个值将导致编译器从那里递增,保证所有枚举值都小于 0。(作为 2s 补码的有符号整数,设置的高位将表示负数)
Likely, the programmers chose this value to be able to send various kinds of events, and shouldn't collide with the others.
很可能,程序员选择这个值是为了能够发送各种事件,并且不应该与其他事件发生冲突。
In a nutshell, don't worry about the actual value; it's just a number. Use the name and understand that's supposed to be the meaning in the context of the calls that use or return those codes.
简而言之,不要担心实际值;这只是一个数字。使用名称并理解这应该是使用或返回这些代码的调用上下文中的含义。
回答by dasblinkenlight
The base type of the enumeration is implementation defined. In this case, the base type should be unsigned int
, because the standard requires the compiler to pick a base type that is wide enough to hold all enumeration values. From the C99 standard, section 6.7.2.2.4:
枚举的基本类型是实现定义的。在这种情况下,基类型应该是unsigned int
,因为标准要求编译器选择一个足够宽的基类型来容纳所有枚举值。来自 C99 标准的第 6.7.2.2.4 节:
Each enumerated type shall be compatible with
char
, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined,108)but shall be capable of representing the values of all the members of the enumeration. The enumerated type is incomplete until after the}
that terminates the list of enumerator declarations.108) An implementation may delay the choice of which integer type until all enumeration constants have been seen.
每个枚举类型应与
char
、有符号整数类型或无符号整数类型兼容。类型的选择是实现定义的,108)但应能够表示枚举的所有成员的值。枚举类型在}
终止枚举器声明列表之前是不完整的。108) 实现可能会延迟选择哪种整数类型,直到看到所有枚举常量。
回答by ldav1s
The underlying type of the enum
depends of the value it needs to hold. The compiler has some latitude on how that type may ultimately be defined. In your case, it's likely that the underlying type of Application_Events
is unsigned int
because it's greater than INT_MAX
, assuming that int
is 32-bits in size (what an enum
is generally). But something like:
的底层类型enum
取决于它需要保存的值。编译器对最终如何定义该类型有一定的自由度。在您的情况下,基础类型可能Application_Events
是unsigned int
因为它大于INT_MAX
,假设它的int
大小为 32 位(enum
通常是什么)。但类似:
enum foo_t {
FOO_Start,
FOO_Thing,
FOO_Another_Thing,
FOO_End
};
The type of enum foo_t
could be int
or unsigned int
.
的类型enum foo_t
可以是int
或unsigned int
。
However, enumeration constants, (e.g., EVENT_EXIT
, FOO_Start
, etc.) are of type int
. That's what you're seeing in the debugger. If you do something like
但是,枚举常量(例如EVENT_EXIT
,FOO_Start
, 等)属于 类型int
。这就是您在调试器中看到的内容。如果你做类似的事情
Application_Events foo = EVENT_EXIT;
the type of foo
could be unsigned
. This question has changed a little, I think, so:
的类型foo
可能是unsigned
. 我认为这个问题发生了一些变化,所以:
1) For iPhone the constant 0x80000000
is probably unsigned
(the iPhone ARM processor has 32-bit int
s). It's value depends on the platform, and the version of C used.
1) 对于 iPhone,常数0x80000000
可能是unsigned
(iPhone ARM 处理器有 32 位int
)。它的价值取决于平台和使用的 C 版本。
2) For practical reality, you can assume that your processor will support two's complement arithmetic, since most platforms use it. The C language itself does notguarantee that however. Other arithmetic schemes (ones' complement, signed magnitude) are allowed.
2) 对于实际情况,您可以假设您的处理器将支持二进制补码算法,因为大多数平台都使用它。然而,C 语言本身并不能保证这一点。允许使用其他算术方案(补码、有符号幅度)。
回答by Ziffusion
- The type for an enum is int.
- 0x80000000 is just a number, but in hexadecimal notation. It's value is whatever you confirmed in the debugger (or any hexadecimal to decimal converter).
- The way enums work is that the values get assigned incrementally from any explicitly assigned value. So, in this case, the enums are getting assigned as EVENT_EXIT=0x80000000, EVENT_TOUCH=0x80000001, EVENT_DRAG=0x80000002, and so on.
- 枚举的类型是 int。
- 0x80000000 只是一个数字,但采用十六进制表示法。它的值是您在调试器(或任何十六进制到十进制转换器)中确认的任何值。
- 枚举的工作方式是从任何显式分配的值中递增分配值。因此,在这种情况下,枚举被分配为 EVENT_EXIT=0x80000000、EVENT_TOUCH=0x80000001、EVENT_DRAG=0x80000002,依此类推。