| Start | ||||||||
|---|---|---|---|---|---|---|---|---|
| Bit-fields A simple bit-field can be used to describe things that may have a specific number of bits involved. In this example we consider an encoder with 23 bits of single precision and 4 bits to describe multi-turn. Bit-fields are often used when interfacing with hardware that outputs data associated with specific number of bits. Another example could be communication with an FPGA, where the FPGA writes data into your memory in 32 bit sections allowing for hardware reads: For this example we have shown a commonly used construct to be able to access the data in its individual bits, or to write the data packet as a whole (emulating what the FPGA might do). We could then access the bits like this: This is valid, but as per the C99 standard 6.7.2.1, item 10: The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined. You need to be aware of endianness when defining bit-fields in this way. As such it may be necessary to use a preprocessor directive to check for the endianness of the machine. An example of this follows: |
||||||||
Using bit-fields as small integers |
||||||||
| Bit-field alignment Bit-fields give an ability to declare structure fields that are smaller than the character width. Bit-fields are implemented with byte-level or word-level mask. The following example results in a structure of 8 bytes. The comments describe one possible layout, but because the standard says the alignment of the addressable storage unit is unspecified, other layouts are also possible. An unnamed bit-field may be of any size, but they can't be initialized or referenced. A zero-width bit-field cannot be given a name and aligns the next field to the boundary defined by the datatype of the bit-field. This is achieved by padding bits between the bit-fields. The size of structure 'A' is 1 byte. In structure B, the first unnamed bit-field skips 2 bits; the zero width bit-field after |
||||||||
| When are bit-fields useful? A bit-field is used to club together many variables into one object, similar to a structure. This allows for reduced memory usage and is especially useful in an embedded environment. If we declare these variables separately, then each has to be at least an 8-bit integer and the total space required will be 5 bytes. Moreover the variables will not use the entire range of an 8 bit unsigned integer (0-255). Here we can use bit-fields. The bit-fields in the structure are accessed the same as any other structure. The programmer needs to take care that the variables are written in range. If out of range the behaviour is undefined. Often the programmer wants to zero the set of bit-fields. This can be done element by element, but there is second method. Simply create a union of the structure above with an unsigned type that is greater than, or equal to, the size of the structure. Then the entire set of bit-fields may be zeroed by zeroing this unsigned integer. Usage is as follows In conclusion, bit-fields are commonly used in memory constrained situations where you have a lot of variables which can take on limited ranges. |
||||||||
Don'ts for bit-fields
Syntax
Parameters
RemarksThe only portable types for bit-fields are Other integer types may be allowed by a specific implementation, but using them is not portable. |
Read Article
https://develop.horje.com/learn/1434/reference