转换位字段为int [英] Converting Bit Field to int
本文介绍了转换位字段为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有位域声明是这样的:
I have bit field declared this way:
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值时,重新presents该位字段的实际值(实际上是某种机器字的,我有它的部分,我希望整个字的INT再presentation)。
I also have int array, and i want to get int value from this array, that represents the actual value of this bit field (which is actually some kind of machine word that i have the parts of it, and i want the int representation of the whole word).
非常感谢。
推荐答案
您可以使用一个联盟:
typedef union bitsetConvertor {
bitset bs;
uint16_t i;
} bitsetConvertor;
bitsetConvertor convertor;
convertor.i = myInt;
bitset bs = convertor.bs;
或者你可以使用一个转换:
Or you can use a cast:
bitset bs = *(bitset *)&myInt;
您也可以在联盟内使用匿名结构:
Or you can use an anonymous struct within a union:
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;
这篇关于转换位字段为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文