0

from my previous experience i understood the following:

// 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

or following:

// 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 _

If i am not right - please correct me.

My embedded-system is 32-bit little-endian. This device have big-endian hardware, connected via SPI. Program, that i must adapt for my system, later work with this hardware in 32-bit big-endian system via parallel bus.

I begin to adapt the library, which intends for building and analyzing ethernet framers and something else.

I met the following code (which crash my mind):

#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

But i met and the following:

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*/

Similar vagueness everywhere in this library. I don't see here logic. Is me really stupid, or this code - nonsense?

Also: Whether I am right in the following:

// 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?

Thanks in advance!

ltWolfik
  • 330
  • 2
  • 9
  • possible duplicate of [Why bit endianness is an issue in bitfields?](http://stackoverflow.com/questions/6043483/why-bit-endianness-is-an-issue-in-bitfields) – Lundin Apr 09 '14 at 14:12
  • The first clause regrarding TTest - false: actually, no, because there could be architectures when goes in order `1234`, other in order `4321`, and other '2143` – user3159253 Apr 09 '14 at 14:13
  • Ok, i understand. If big-endian uses `4321` and little-endian uses `1234` whether there will be it truth? – ltWolfik Apr 11 '14 at 11:48

1 Answers1

0

In this particular case, the structures are big- and little-endian aligned on 16 bit boundaries. If you group each set into 16 bit pieces, you can see that is the size of the things being swapped for endian compatibility.

JohnH
  • 2,713
  • 12
  • 21
  • Yes, i can see it, but for 'SERVICE_SPEC_OLD_FIELD_T' struct is not truth - It seems, that It's used 32 bit-pieces. Yes? – ltWolfik Apr 11 '14 at 11:40