| Start |
|---|
Declaration and Usage Here, each of these two fields will occupy 1 bit in memory. It is specified by If more bits are required, replace "1" with number of bits required. For example: The whole structure is using just 22 bits, and with normal compiler settings, Usage is pretty simple. Just declare the variable, and use it like ordinary structure. RemarksHow expensive are the bitwise operations? Suppose a simple non-bit field structure: In some later code: If This is straightforward because x is not co-mingled with y. But imagine redefining the structure with bit fields: Both This may be a good trade-off if we have thousands or millions of these structures, and it helps keeps memory in cache or prevents swapping—or could bloat the executable to worsen these problems and slow processing. As with all things, use good judgement. Device driver use: Avoid bit fields as a clever implementation strategy for device drivers. Bit field storage layouts are not necessarily consistent between compilers, making such implementations non-portable. The read-modify-write to set values may not do what devices expect, causing unexpected behaviors. |
Read Article
https://develop.horje.com/learn/1434/reference