English 中文(简体)
2. en类的比水行动
原标题:Bitwise operations on enum types

在试图通过以下法典将记忆中的数据读成一卷时,我得出了这些重要成果:

void read_memory (const unsigned, const unsigned, unsigned* const);

/* ... */

enum {DATA_0, DATA_1, DATA_2} data;
read_memory(base_addr, offset, &data);  //data = 0x0900
data >>= 8;  //data = 0x7e000000

我在此工作时采用了一种临时的未签署类型。 但我很想知道,以前的方法为何不可行。

First of all, I m aware that the standard does not require a specific width for enum types, as long as all members can be represented. As a matter of fact, 6.7.2.2 states that:

Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type.

But since the raw data read from memory fits in a char, I think it should not be an issue. Moreover, if I understand correctly, "compatible" means that you can use it as if it were of such a type. In particular, objects of enumerated type can be operands of bitwise shift operators. I m also aware that signedness may be a problem as we don t know if enum are signed or not. But, as far as I can tell, 0x0900 does not appear to be signed.

因此,问题何在?

最佳回答

在抽烟外测试该守则之后,它发现该守则的行为是正确的,用 g布显示的信息是不准确的。

In fact, analyzing the disassembled machine code revealed that it did not match C source line numbers. As a result, the epilogue of the function was thought (by gdb) to be part of the data >>= 8; instruction. This issue remains strange to me since I compiled and linked all source and object files with -g -O0.

也许汇编者中的一杯......

Edit:实际上,它与gdb没有任何关系,这是汇编者所排放的丑化标志是错误的。

问题回答

你们(我认为,有坏名)读到多少数据? 如果与<>数据编码>的数值相同,则你有问题。 仅仅因为所列举的数值小到<条码>。 <><>has >到<> > > 仅选一个char <>/code>; 或许操作的指令(<>int-型号>的编号更快,而且(或)存在一致性问题。

签署“Enum”型号,因此,当你把标书(1)放在数字轨道上。





相关问题
Finding the Highest Value in an Enumeration

I m writing a method which determines the highest value in a .NET enumeration so I can create a BitArray with one bit for each enum value: pressedKeys = new BitArray(highestValueInEnum<Keys>());...

Conversion of Enum to Enumerable

To convert Enum to Enumerable ,I use public enum Flags { Trivial=1, Minor, Major, Critical } IEnumerable<int> n = Enumerable.Range((int)Flags.Trivial, (...

Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

Enum scoping issues

I try to keep things as local as possible, so I put enums at class scope, even if they are shared between two classes (I put it in the class that "goes better" with it.) This has worked out great, but ...

How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

C++ enum value initialization

I have an enum declared in my code as: enum REMOTE_CONN { REMOTE_CONN_DEFAULT = 0, REMOTE_CONN_EX_MAN = 10000, REMOTE_CONN_SD_ANNOUNCE, REMOTE_CONN_SD_IO, REMOTE_CONN_AL, ...

WCF: Enforce equal DataContracts on both sides

I m wondering if it is possible to have WCF make sure that the DataContracts on both sides of a connection are exactly the same (and throw an exception when trying to connect if they are not). For ...