【高手】小字节序转为大字节序函数
【高手求助】小字节序转为大字节序函数昨天的一道笔试题,编写如下结构的小字节序转大字节序的转换函数:void
【高手求助】小字节序转为大字节序函数
昨天的一道笔试题,编写如下结构的小字节序转大字节序的转换函数:void ConvertLToB(T_Sample*ptIn),结构的定义如下
C/C++ code#pragma pack(4)typedef struct tagT_Sample{BYTE ucA :3;BYTE ucB :3;BYTE ucC :3;BYTE ucD :3;BYTE ucE :3;DWORD dwF :24;}T_Sample;
自己对大小端也有点了解,但是也只是限于判断和求个输出什么的,哎,怪自己学艺不精,求高手来解答!
[解决办法]#pragma pack(4)
typedef struct tagT_Sample{
////1字节//////
BYTE ucA :3;
BYTE ucB :3;
//////////////
////1字节/////
BYTE ucC :3;
BYTE ucD :3;
//////////////
///1字节//////
BYTE ucE :3;
//////////////
//填充1字节///
/////////////
/////4字节////
DWORD dwF :24;
/////////////
}T_Sample;
就这么8个字节, SB面试官是打算让你把整个结构体的字节颠倒一下还是把DWORD字节的字节序颠倒一下(唯一的多字节),还是把每个域里的位颠倒一下?
[解决办法]出题的就是个半桶水!
按照标准sizeof(T_Sample)都是不确定的!
对于位字段来说,几乎所有的描述都是不确定的
implementation-defined或者
unspecified.
A bit-field may have type int, unsigned int, or signed int. Whether the high-order bit position of a “plain” int bit-field is treated as a sign bit is
implementation-defined. A bit-field is interpreted as an integral type consisting of the specified number of bits.
An implementation may allocate any addressable storage unit large enough to hold a bit-field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is
implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is
implementation-defined. The alignment of the addressable storage unit is
unspecified.