Home > Backend Development > C++ > Bit fields in C

Bit fields in C

WBOY
Release: 2023-09-08 22:57:03
forward
1437 people have browsed it

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;
Copy after login

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;
Copy after login

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;
}
Copy after login

Output

Memory size occupied by status1 : 8
Memory size occupied by status2 : 4
Copy after login

Bitfield Declaration:

Bitfield declarations have the following form within the structure -

struct {
   type [member_name] : width ;
};
Copy after login

The following table describes the variable elements of the bitfields-

Elements Description
typeThe integer type that determines how bit field values ​​are interpreted. The type can be int, signed int, or unsigned int.
member_nameThe name of the bit field.
WidthThe 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;
Copy after login

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;
}
Copy after login

Output

Sizeof( Age ) : 4
Age.age : 4
Age.age : 7
Age.age : 0
Copy after login

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

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template