我有这样一种方式:
typedef struct morder {
unsigned int targetRegister : 3;
unsigned int targetmethodOfAddressing : 3;
unsigned int originRegister : 3;
unsigned int originMethodofAddressing : 3;
unsigned int oCode : 4;
} bitset;
我也有int数组,我想从这个数组中获取int值,表示这个位字段的实际值(这实际上是我拥有它的一部分的一些机器字,我想要int的表示形式整个字).
非常感谢.
解决方法
你可以使用联合:
typedef union bitsetConvertor {
bitset bs;
uint16_t i;
} bitsetConvertor;
bitsetConvertor convertor;
convertor.i = myInt;
bitset bs = convertor.bs;
或者你可以使用一个演员:
bitset bs = *(bitset *)&myInt;
或者你可以使用联合中的匿名结构:
typedef union morder {
struct {
unsigned int targetRegister : 3;
unsigned int targetmethodOfAddressing : 3;
unsigned int originRegister : 3;
unsigned int originMethodofAddressing : 3;
unsigned int oCode : 4;
};
uint16_t intRepresentation;
} bitset;
bitset bs;
bs.intRepresentation = myInt;