首页 文章

在big-endian和little-endian中对齐混合位字段和结构字段

提问于
浏览
0

根据我以前的经验,我理解以下内容:

// if i have structure in big-endian system, look like this:
typedef struct
{
    unsigned long
        a: t1,
        b: t2,
        c: t3,
        d: t4,
        //...
        z: tn;
} TType;

// i can adapt this for little-endian so:
typedef struct
{
    unsigned long
        z:tn,
        //...
        d: t4,
        c: t3,
        b: t2,
        a: t1;
} TType;

// and i get identical mapping to memory

或以下:

// if i have next structure:

typedef struct
{
    unsigned long
        a : 2,
        b : 5,
        c : 6,
        d : 3;
} TType2;

// ...

TType2 test;

test.a = 0x2;
test.b = 0x0E;
test.c = 0x3A;
test.d = 0x6;

printf("*(unsigned short *)&test = 0x%04X\n", *(unsigned short *)&test);

// in little-endian system i get: 0xDD3A , or mapping to memory :
//   c'-|-----b-----|-a-|   |--d--|------c----
//     0 0 1 1 _ 1 0 1 0 _|_ 1 1 0 1 _ 1 1 0 1 _

// in big-endian system i get: 0xD69D , or mapping to memory :
//     -----c-----|--d--|   |-a-|------b----|-c'
//     1 1 0 1 _ 0 1 1 0 _|_ 1 0 0 1 _ 1 1 0 1 _

如果我不对 - 请纠正我 .

我的嵌入式系统是32位小端 . 该器件具有大端硬件,通过SPI连接 . 程序,我必须适应我的系统,以后通过并行总线在32位大端系统中使用这个硬件 .

我开始调整库,该库旨在构建和分析以太网制作器和其他东西 .

我遇到了以下代码(让我心烦意乱):

#pragma pack(1)

//...
typedef unsigned short word;
//...

#ifdef _MOTOROLA_CPU
typedef struct
{

    word        ip_ver      : 4;
    word        ihl         : 4;
    word        ip_tos      : 8;

    word        tot_len;
    word        identification;

    word        flags       : 3;
    word        fragment_ofs: 13;
    word        time_to_live: 8;
    word        protocol    : 8;

    word        check_sum;

    IP_ADDRESS_T    src;
    IP_ADDRESS_T    dest;

} IP_MSG_HEADER_T, *IP_MSG_HEADER_P;

#else // Intel CPU.
typedef struct
{

    word        ip_tos      : 8;
    word        ihl         : 4;
    word        ip_ver      : 4;

    word        tot_len;
    word        identification;

    word        fragment_ofs: 13;
    word        flags       : 3;
    word        protocol    : 8;
    word        time_to_live: 8;

    word        check_sum;

    IP_ADDRESS_T    src;
    IP_ADDRESS_T    dest;

} IP_MSG_HEADER_T, *IP_MSG_HEADER_P;
#endif

但我遇到了以下情况:

typedef struct
{

    word    formid          : 5;
    word    padding_formid  : 3;
    word    TS_in_bundle    : 5;
    word    padding_ts      : 3;
    word    cell_per_frame  : 8;
    word    padding         : 8;

} SERVICE_SPEC_OLD_FIELD_T, *SERVICE_SPEC_OLD_FIELD_P
#else // Intel CPU.
typedef struct
{

    word    padding         : 8;
    word    cell_per_frame  : 8;
    word    padding_ts      : 3;
    word    TS_in_bundle    : 5;
    word    padding_formid  : 3;
    word    formid          : 5;

} SERVICE_SPEC_OLD_FIELD_T, *SERVICE_SPEC_OLD_FIELD_P;
#endif /*_MOTOROLA_CPU*/

这个图书馆里到处都是类似的模糊 . 我没有看到这里的逻辑 . 我真的很蠢,还是这段代码 - 胡说八道?

另外:我是否正确如下:

// Two structures
typedef struct
{
    unsigned long
        a : 1,
        b : 3,
        c : 12;
    unsigned short word;
} Type1;
// and
typedef struct
{
    unsigned long
        a : 1,
        b : 3,
        c : 12,
        word : 16;
} Type2;
// will be identical in memory for little-endian and different for big-endian?

提前致谢!

1 回答

  • 0

    在这种特殊情况下,结构是在16位边界上对齐的大端和小端 . 如果将每个组分组为16位,则可以看到为了 endpoints 兼容性而交换的内容的大小 .

相关问题