Site Loader

In C language structure and union support a very important feature that is the bit-field. The bit-fields allow the packing of data in a structure and prevent the wastage of memory.Note: The layout of bit fields in a C struct is implementation-defined that is the reason a lot of people are avoiding the use of bit-filed.Syntax:In C language declaration of the bit-field structure or union is similar to the declaration of the normal structure or union, the main difference is that bit-field member is declared with aspecified number of bits preceded by the colon.struct {type-specifier declarator opt: constant-expression};In the above declaration, constant-expression specifies the width of the field in bits and must be a non-negative integer value. If the value is zero, the declaration has no declarator.The type-specifier for the declarator must be _Bool, signed int, unsigned int, or some other implementation-de?ned type.It is implementation-de?ned whether atomic types are permitted.Example,Let’s take an example to understand the structure bit field.struct packed_data {unsigned int data1:1;unsigned int data2:1;unsigned int data3:1;unsigned int data4:1;unsigned int data5:1;unsigned int data6:4;unsigned int data7:9;} sPackData;In below statement, structure packed_data contains 7 members. In which five-member (data1 to data5) has 1 bit and 6th and 7th member has the 4 and 9 bits.Way to access the member:sPackData.data5 = 9;Practical use of bit field in C?Suppose a microcontroller has a port of 8 pins and each pin are connected to the led. In that scenario using the bitfield, we can easily change the status of the led.So first we need to create a bit-field structure to mapping with the micro-controller port. typedef union {        struct {                        uint8_t LED1 : 1;            uint8_t LED2 : 1;            uint8_t LED3 : 1;            uint8_t LED4 : 1;            uint8_t LED5 : 1;            uint8_t LED6 : 1;            uint8_t LED7 : 1;            uint8_t LED8 : 1;        };        uint8_t AllLedState;}LED_BAR_STATE; typedef union {         struct {                        uint8_t LED1 : 1;            uint8_t LED2 : 1;            uint8_t LED3 : 1;            uint8_t LED4 : 1;            uint8_t LED5 : 1;            uint8_t LED6 : 1;            uint8_t LED7 : 1;            uint8_t LED8 : 1;        };         uint8_t AllLedState; }LED_BAR_STATE;Create a pointer to the above describe bit-field and assign the address of the PORT to the pointer which you want to access.volatile LED_BAR_STATE *pLedState = (volatile LED_BAR_STATE *)0xE002C000;Now you can access the individual led using the pointer.pLedState->LED1 = 1;pLedState->LED2 = 0;Note: Here, I am only describing, how is the bit-field work. I am not suggesting to use bit-field in the mapping of a hardware register because the allocation of bit-field depends upon the compiler.Might be the result of one compiler can be different from another compiler. So we should avoid the compiler-dependent code, In simple word, avoid using bit fields for the mapping of the hardware register.Some important points about bit fields in C.If we are compiled the same C program that uses the bit-field on a different system, the result of the program may vary (C program may not work properly).The order of allocation of bit-?elds within a unit low-order to high-order or high-order to low-order is implementation-de?ned.#include struct sBitField{   unsigned int  Data00:8;   unsigned int  Data01:8;   unsigned int  Data02:8;   unsigned int  Data03:8;};//function to print in bitsvoid printBits( int a ){   int i;    for ( i = 31; i >= 0; i– )   { if ( (a & (1< struct sBitField{   unsigned int  Data00:8;   unsigned int  Data01:8;   unsigned int  Data02:8;   unsigned int  Data03:8; };  //function to print in bitsvoid printBits( int a ){   int i;    for ( i = 31; i >= 0; i– )   { if ( (a & (1<struct sData{   unsigned int a: 2;   unsigned int b: 2;   unsigned int c: 2;};int main(){   struct sData data;      data.a = 5;      printf(“%d”, data.a );      return 0;}#include  struct sData{   unsigned int a: 2;   unsigned int b: 2;   unsigned int c: 2;};  int main(){   struct sData data;      data.a = 5;      printf(“%d”, data.a );      return 0;}Output:Implementation-DependentWe can not create a pointer to the bit-field and also not use the address-of operator (&) to the bit-field member.#include struct sData{   unsigned int a: 2;   unsigned int b: 2;   unsigned int c: 2;};int main(){   struct sData data;      data.a = 2;      printf(“Address of data.a =  %p”, &data.a );      return 0;}#include  struct sData{   unsigned int a: 2;   unsigned int b: 2;   unsigned int c: 2;};  int main(){   struct sData data;      data.a = 2;      printf(“Address of data.a =  %p”, &data.a );      return 0;}Output:Error cannot take address of bit-field ‘a’We can not create an array of bit fields.#include struct sData{   unsigned int a: 2;   unsigned int b5: 2;};int main(){   struct sData data;      data.a = 2;       return 0;}#include  struct sData{   unsigned int a: 2;   unsigned int b5: 2;};  int main(){   struct sData data;      data.a = 2;       return 0;}Output:Error bit-field ‘b’ has an invalid type.The bit fields must also be long enough to contain the bit pattern. See the below example,struct sData{   unsigned int a: 2;      short b: 17; /* Illegal! */       unsigned int c: 2;};struct sData{   unsigned int a: 2;      short b: 17; /* Illegal! */       unsigned int c: 2;};The alignment of the addressable storage unit is unspeci?ed.If enough space remains, a bit-?eld that immediately follows another bit-?eld in a structure shall be packed into adjacent bits of the same unit.A bit-?eld declaration with no declarator is called unnamed bit field. If the width of the unnamed bit-field is 0 (zero), it indicates that no further bit-?eld is to be packed into the unit in which the previous bit?eld, if any, was placed.See the below example, here I have created two structure. In the second structure, I am using the unnamed bit-field with 0 widths for the force unalignment.#include // A structure without forced alignmenttypedef struct{   unsigned int data1: 5;   unsigned int data2: 8;}sData1; // A structure with forced alignmenttypedef struct{   unsigned int data1: 5;   unsigned int: 0;   unsigned int data2: 8;}sData2;  int main(){   printf(“Size of sData1 = %d
“, sizeof(sData1));      printf(“Size of sData2 = %d
“, sizeof(sData2));      return 0;}#include  // A structure without forced alignmenttypedef struct{   unsigned int data1: 5;   unsigned int data2: 8;}sData1;  // A structure with forced alignmenttypedef struct{   unsigned int data1: 5;   unsigned int: 0;   unsigned int data2: 8;}sData2;  int main(){   printf(“Size of sData1 = %d
“, sizeof(sData1));      printf(“Size of sData2 = %d
“, sizeof(sData2));      return 0;}Output:Size of sData1 = 4Size of sData2 = 8

Post Author: admin

x

Hi!
I'm Katherine!

Would you like to get a custom essay? How about receiving a customized one?

Check it out