What are bit fields What is the use of bit fields in a Structure declaration?

Answer:

Answer

Both C and C++ allow integer members to be stored into memory spaces smaller than the compiler would ordinarily allow. These space-saving structure members are called bit fields, and their width in bits can be explicitly declared.

Gagandeep Singh

Bitfields can only be declared inside a structure or a union, and allow you to specify some very small objects of a given number of bits in length.

struct {

/* field 4 bits wide */

unsigned field1 :4;

/*

* unnamed 3 bit field

* unnamed fields allow for padding */

unsigned :3;

/*

* one-bit field

* can only be 0 or -1 in two's complement! */

signed field2 :1;

/* align next field on a storage unit */

unsigned :0;

unsigned field3 :6; }full_of_fields;

The main use of bitfields is either to allow tight packing of data or to be able to specify the fields within some externally produced data files.

First answer by Rohiniskumar. Last edit by Gagan2004 2005. Contributor trust: 2 [recommend contributor recommended]. Question popularity: 12 [recommend question].