Structure layout in big/little endian modes
Ian Lance Taylor
iant@google.com
Wed Nov 23 22:10:00 GMT 2011
Владимир Андреев <volodya@netfolder.ru> writes:
> I have some lack of understanding how to write endianness independent
> definition of structure, which contains bit fields.
>
> For example, field, containing endpoint address in endpoint descriptor
> of USB device, can be defined as follow:
>
> struct EndpointAddress
> {
> UnsignedByte EndpointNumber: 4;
> UnsignedByte: 3;
> UnsignedByte TransferDirection: 1;
> };
>
> All USB descriptors have little endian byte order (and filling
> starting from least significant bit). If I would run some code which
> uses such definition on big endian CPU, I will get incorrect result.
>
> Can I tell GCC to use for layout big/little endian mode without manual
> changing structure layout for target CPU?
If you want to make your code work on different systems, even ones that
don't differ in endianness, don't use bitfields. Use <<, >>, &, |.
Ian
More information about the Gcc-help
mailing list