将位集保存到结构体字段中
我决定不使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
std::bitset
的成员函数to_ulong
,它将集合中的位作为unsigned long
返回。Use
std::bitset
's member functionto_ulong
, which returns the bits in the set as anunsigned long
.