Access Bit Fields in C

I have a structure that is case-sensitive on hardware.

typedef unsigned int uint32; typedef union A_u{ uint32 words[4]; struct { uint32 a : 2; uint32 b : 3; uint32 unused : 27; uint32 c : 2; uint32 d : 3; uint32 unused0 : 27; uint32 unused1 : 1; uint32 e : 1; uint32 f : 1; uint32 g : 1; uint32 h : 1; uint32 i : 1; uint32 unused2 : 26; uint32 reserved6 : 32; }s; }A_t; main() { A_t obj; uint32 val = 1; memset(&obj, 0, sizeof(A_t)); //fills data read_data(&obj); printf("0x%x\n", obj.words[2]); printf("obj.sh = %d\n", obj.sh); } 

Exit

0x80000000
obj.sh = 1.

Although the third word is 0x80000000, obj.sh is shown as 1. I cannot figure it out. I run this on powerpc, where the first bit field is the most significant.

+4
source share
1 answer

Compilers are free to do what they like, as the memory locations coincide with members of structures and associations and other complex data structures.

I saw cryptic runtime errors using structures that were caused by boundary alignment problems because one part of the source saw the structure in one direction and the other saw it in a different way.

C basically uses structure as a template for overlaying a region of memory, and if different files see the structure differently, one piece of code calculates the address offset for a structure element that is different from another.

Some compilers will provide a pragma that allows you to specify the action of the package so that you can specify the alignment boundary of the structure members.

To maintain hardware compatibility, the best way is to use unsigned character arrays, because in this way you can index an element without an unsigned character, and then select one of eight bits with bitwise operators. You can then wrap an array of unsigned characters in various access functions, classes, macros, etc., to manipulate the actual data that you want to read and write. Using unsigned character arrays is also more portable.

The bit fields are apparently more suitable for the application to maintain indicators and flags in memory saving mode, but are not so useful for communicating with objects (hardware or software) outside the application. And depending on the compiler and the hardware on which the application is running, the use of bit fields can lead to a small penalty for execution, since access to bits is applied to them, they are packed and unpacked.

+4
source

All Articles