按位上下文中的 "straddle" 是什么意思?
What does "straddle" in a Bitwise Context mean?
我正在阅读有关位域的信息:
http://en.cppreference.com/w/cpp/language/bit_field。
文章提到了 "straddl(e)"ing 位。
示例上下文包括...
"Adjacent bit field members may be packed to share and straddle the
individual bytes."
这个词在位域中是什么意思?
我在写页面时用那个词来指代这种情况,正如评论中正确指出的那样,如该页面的第二个示例所示:
#include <iostream>
struct S {
// will usually occupy 2 bytes:
// 3 bits: value of b1
// 2 bits: unused
// 6 bits: value of b2
// 2 bits: value of b3
// 3 bits: unused
unsigned char b1 : 3, : 2, b2 : 6, b3 : 2;
};
int main()
{
std::cout << sizeof(S) << '\n'; // usually prints 2
}
这里(假设sizeof(S)
为2)字段b2
长6位,前3位在第一个字节,后3位在第二个字节。它跨越两个字节。 (下一个示例显示如何将所有 6 位强制为一个字节)
我正在阅读有关位域的信息: http://en.cppreference.com/w/cpp/language/bit_field。
文章提到了 "straddl(e)"ing 位。 示例上下文包括...
"Adjacent bit field members may be packed to share and straddle the individual bytes."
这个词在位域中是什么意思?
我在写页面时用那个词来指代这种情况,正如评论中正确指出的那样,如该页面的第二个示例所示:
#include <iostream>
struct S {
// will usually occupy 2 bytes:
// 3 bits: value of b1
// 2 bits: unused
// 6 bits: value of b2
// 2 bits: value of b3
// 3 bits: unused
unsigned char b1 : 3, : 2, b2 : 6, b3 : 2;
};
int main()
{
std::cout << sizeof(S) << '\n'; // usually prints 2
}
这里(假设sizeof(S)
为2)字段b2
长6位,前3位在第一个字节,后3位在第二个字节。它跨越两个字节。 (下一个示例显示如何将所有 6 位强制为一个字节)