将位集保存到结构体字段中

发布于 2024-11-25 20:19:03 字数 1038 浏览 0 评论 0原文

我决定不使用 MACROS 进行按位运算,而是使用 BitSet。基本上我想做的是,我收到一个结构评估其位,然后将它们附加到另一个结构。

我收到一个结构,说:

typedef struct{
uint8 status; //!< Status
} MsgStatus;

我需要获取状态并检查每个接收到的位,因此我创建了接收到的结构的位集:

m_msgBits = new MsgStatus();
bitset<8> msgBits(m_msgBits->status);
// I evaluate the bits 

现在,在评估之后,我需要将接收到的位附加到另一个结构,说:

typedef struct{
uint32 status; //!< Status
} MsgOverallStatus;

所以,什么我做的是:

m_OverallStatus = new MsgOverallStatus();
bitset<16> overallBits(m_OverallStatus->status);
m_OverallStatus.reset(); // 00000000 00000000

//Then append bits in msgBits in overallBits, for example:
overallBits.set(0, msgBits[0]);
overallBits.set(1, msgBits[1]);
overallBits.set(2, msgBits[2]);

//==== HERE WHERE I DUNNO HOW TO DO IT ====
m_OverallStatus->status = overallBits;

我想将位分配给结构字段,我收到此错误:无法转换 'std::bitset<16u>'到赋值中的“uint16”

我不想更改结构体字段类型,那么我该怎么办?我为我的问题多么愚蠢而道歉。

提前致谢

I decedied not to use MACROS for the bitwise operations but rather use BitSet. Basically what i am intending to do is, I receive a struct evaluate its bits then appened them to another struct.

I receive a Struct, say:

typedef struct{
uint8 status; //!< Status
} MsgStatus;

I need to get the status and check each received bit, so i create a bitset of the recevied struct:

m_msgBits = new MsgStatus();
bitset<8> msgBits(m_msgBits->status);
// I evaluate the bits 

Now, after the evaluation I need to appened the received bits to another struct, say:

typedef struct{
uint32 status; //!< Status
} MsgOverallStatus;

So, what i do is:

m_OverallStatus = new MsgOverallStatus();
bitset<16> overallBits(m_OverallStatus->status);
m_OverallStatus.reset(); // 00000000 00000000

//Then append bits in msgBits in overallBits, for example:
overallBits.set(0, msgBits[0]);
overallBits.set(1, msgBits[1]);
overallBits.set(2, msgBits[2]);

//==== HERE WHERE I DUNNO HOW TO DO IT ====
m_OverallStatus->status = overallBits;

I want to assign the bits to the struct field, i get this error: cannot convert ‘std::bitset<16u>’ to ‘uint16’ in assignment

I dont want to change the struct field type, so what can I do? I apologize for how silly is my question.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把昨日还给我 2024-12-02 20:19:04

使用std::bitset的成员函数to_ulong,它将集合中的位作为unsigned long返回。

Use std::bitset's member function to_ulong, which returns the bits in the set as an unsigned long.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文