【发布时间】:2010-09-03 16:37:24
【问题描述】:
我在位域和字节序方面遇到了一些麻烦... 我很困惑。
我需要解析一些从网络获取的数据,发送的是 lil endian(我使用 boost::asio)
你能解释一下吗
struct TEST
{
unsigned short _last : 1;
unsigned short _ID : 6;
unsigned short _LENGH : 9;
};
struct TEST2
{
unsigned short _LENGH:9 ;
unsigned short _ID:6 ;
unsigned short _last:1 ;
};
int main(int argc, char* argv[])
{
printf("Hello World!\n");
TEST one;
one._ID = 0;
one._last = 0;
one._LENGH = 2; //the value affected here is always divided by 2, it is multiplied by 2 when i cast a short to this structure
TEST2 two;
two._ID = 0;
two._last = 0;
two._LENGH = 2; //the value here is well stored
bit_print((char*)&one,2);
bit_print((char*)&two,2);
return 0;
}
[输出]
00000000 00000001
00000010 00000000
【问题讨论】:
-
bit_print()是否经过良好测试? -
可能无关紧要,但
_ID和_LENGTH是保留的;您不应使用以__或_开头的名称和大写字母,以防实现定义具有相同名称的宏。 -
bit_print 在这里被用来试图查看内存中发生了什么。事实上,如果当我通过网络发送这些结构时,我认为它被除以二。
标签: c++ endianness bit-fields