微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将位域转换为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;

原文地址:https://www.jb51.cc/c/112685.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐