Home  >  Article  >  Backend Development  >  Bit fields in C

Bit fields in C

WBOY
WBOYforward
2023-09-08 22:57:031359browse

Bit fields in C

In this section, we will understand what is a bit field in C language.

Suppose your C program contains many TRUE/FALSE variables grouped in a structure called state as follows -

struct {
   unsigned int widthValidated;
   unsigned int heightValidated;
} status;

The structure requires 8 bits of memory space, but in reality, We will store 0 or 1 in each variable. In this case, C programming language provides a better way to utilize the memory space.

If you use such variables in a structure, you can define the width of the variable to tell the C compiler that you are using only these number of bits. For example, the above structure can be rewritten as follows -

struct {
   unsigned int widthValidated : 1;
   unsigned int heightValidated : 1;
} status;

The above structure requires memory space for 4 bits of state variables but uses only 2 bits to store the value.

If a maximum of 32 variables are used, each variable is 1 bit wide, then the status structure will also use 4 bits. However, once you have 33 variables, it allocates the next slot of memory and starts using 8 bits. Let us check the following example to understand this concept -

Sample Code

Live Demonstration

#include <stdio.h>
#include <string.h>
/* define simple structure */
struct {
   unsigned int widthValidated;
   unsigned int heightValidated;
} status1;
/* define a structure with bit fields */
struct {
   unsigned int widthValidated : 1;
   unsigned int heightValidated : 1;
} status2;
int main( ) {
   printf( "Memory size occupied by status1 : %d</p><p>", sizeof(status1));
   printf( "Memory size occupied by status2 : %d</p><p>", sizeof(status2));
   return 0;
}

Output

Memory size occupied by status1 : 8
Memory size occupied by status2 : 4

Bitfield Declaration:

Bitfield declarations have the following form within the structure -

struct {
   type [member_name] : width ;
};

The following table describes the variable elements of the bitfields-

Elements Description th>
type The integer type that determines how bit field values ​​are interpreted. The type can be int, signed int, or unsigned int.
member_name The name of the bit field.
Width The number of digits in the bit field. The width must be less than or equal to the bit width of the specified type.

A variable defined with a predefined width is called a bit field. A bitfield can hold multiple bits; for example, if you need a variable to store values ​​from 0 to 7, then you can define a bitfield with a width of 3 bits as shown below -

struct {
   unsigned int age : 3;
} Age;

above The structure definition instructs the C compiler that the age variable will only use 3 bits to store the value. If you try to use more than 3 bits then it will not allow you to do so. Let us try the following example -

Sample Code

Live Demonstration

#include <stdio.h>
#include <string.h>
struct {
   unsigned int age : 3;
} Age;
int main( ) {
   Age.age = 4;
   printf( "Sizeof( Age ) : %d</p><p>", sizeof(Age) );
   printf( "Age.age : %d</p><p>", Age.age );
   Age.age = 7;
   printf( "Age.age : %d</p><p>", Age.age );
   Age.age = 8;
   printf( "Age.age : %d</p><p>", Age.age );
   return 0;
}

Output

Sizeof( Age ) : 4
Age.age : 4
Age.age : 7
Age.age : 0

The above is the detailed content of Bit fields in C. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete