Bit field in C

Suppose your C program includes a number of TRUE / FALSE variables grouped in a structure called a page to check if the manufactured goods have sufficient width and height allowed.

Suppose your C program includes a number of TRUE / FALSE variables grouped in a structure called a page to check if the manufactured goods have sufficient width and height allowed, as follows:

 struct { unsigned int chieurong ; unsigned int chieucao ; } trangthai ; 

This structure requires 8 bytes of memory but in fact it reserves 0 or 1 bytes per variable. C programming language has a way to optimize memory in this case. You are using variables within the structure then you can define the size of the variables, it will notify the C compiler that only the number of bytes is used. For example, the above structure can be rewritten as follows:

 struct { unsigned int chieurong : 1 ; unsigned int chieucao : 1 ; } trangthai ; 

Now the above structure will require 4 bytes of memory for the page variable, but only 2 bits are used to store the value. You must use up to 32 variables with this length of 1 bit, so this structure will use 4 bytes and when you have 33 variables, it will allocate the next value in memory and start using 8 bytes. Now let's examine the example below to understand this definition.

 #include #include /* dinh nghia mot cau truc don gian */ struct { unsigned int chieurong ; unsigned int chieucao ; } trangthai1 ; /* dinh nghia mot cau truc voi cac truong bit */ struct { unsigned int chieurong : 1 ; unsigned int chieucao : 1 ; } trangthai2 ; int main ( ) { printf ( "Bo nho bi chiem giu boi trangthai1 la: %dn" , sizeof ( trangthai1 )); printf ( "Bo nho bi chiem giu boi trangthai2 la: %dn" , sizeof ( trangthai2 )); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

Compiling and running the above C program will result:

Bit field in C Picture 1Bit field in C Picture 1

Declare Bit Field in C

Declare a Bit Field inside a structure with the following pattern:

 struct { kieu_du_lieu [ ten_thanh_vien ] : do_rong ; }; 

Below is a description for the variable elements in a Bit Field:

ElementDescriptionkieu_du_lieu An integer type can determine how the Bit field is interpreted. This type can be int, signed int, unsigned int ten_thanh_vien Name of Bit Field. do_rong (is the width) The number of bits contained in a field. The length must be less than or equal to the Bit Field length of a specific object.

A variable defined with a given magnitude value is called a Bit field . A Bit Field can store more than one single bit eg you need a variable to store values ​​from 0 to 7, then you can define the Bit Field with a maximum length of 3 bits as follows:

 struct { unsigned int tuoi : 3 ; } Tuoi ; 

The above definition will instruct the C compiler that the variable will use 3 bits to store values, if you use more than 3 bits it will not allow you to do so. Now try the example below:

 #include #include struct { unsigned int tuoi : 3 ; } Tuoi ; int main ( ) { Tuoi . tuoi = 3 ; printf ( "Bo nho bi chiem giu boi Tuoi la Sizeof( Tuoi ) = %dn" , sizeof ( Tuoi ) ); printf ( "Tuoi.tuoi : %dn" , Tuoi . tuoi ); Tuoi . tuoi = 6 ; printf ( "Tuoi.tuoi : %dn" , Tuoi . tuoi ); Tuoi . tuoi = 7 ; printf ( "Tuoi.tuoi : %dn" , Tuoi . tuoi ); /* Bay gio chung ta thu in nhieu hon 3 bit */ printf ( "n-----------------------n" ); Tuoi . tuoi = 8 ; printf ( "Tuoi.tuoi : %dn" , Tuoi . tuoi ); Tuoi . tuoi = 9 ; printf ( "Tuoi.tuoi : %dn" , Tuoi . tuoi ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

Compiling and running the above C program will result:

Bit field in C Picture 2Bit field in C Picture 2

According to Tutorialspoint

Previous post: Union in C

Next lesson: Keyword typedef in C

4.4 ★ | 5 Vote