将位域转换为int
发布时间:2020-12-16 03:01:22 所属栏目:百科 来源:网络整理
导读:我有这样一种方式: 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数
我有这样一种方式:
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; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |