0% found this document useful (0 votes)
61 views2 pages

Bit Field in C

Ho to bit filed in c

Uploaded by

jmpmys
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views2 pages

Bit Field in C

Ho to bit filed in c

Uploaded by

jmpmys
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Bit Field in C

In C, we can specify size of structure and union members in bits to optimize memory space. The idea is to
use memory efficiently when we know that the value of a field or group of fields will never exceed a limit
or is within a small range. For example, we know that day value of a date cannot exceed 31, month of
date cannot exceed 12, etc.

Bit Field Declaration

The declaration of a bit-field has the following form inside a structure −


struct
{
type [member_name] : width ;
};
The following table describes the variable elements of a bit field where−
type : An integer type that determines how a bit-field's value is interpreted. The type may be int, signed
int, or unsigned int.
member_name : The name of the bit-field.
Width : The number of bits in the bit-field. The width must be less than or equal to the bit width of the
specified type.
For example, consider the following declaration of date without the use of bit fields.
#include <stdio.h>
// A simple representation of the date
struct date {
unsigned int d;
unsigned int m;
unsigned int y;
};
int main()
{
printf("Size of date is %lu bytes\n", sizeof(struct date));
struct date dt = { 31, 12, 2014 };
printf("Date is %d/%d/%d", dt.d, dt.m, dt.y);
}
Output:
Size of date is 12 bytes
Date is 31/12/2014
The above representation of ‘date’ takes 12 bytes on a compiler where an unsigned int takes 4 bytes.
Since we know that the value of ‘d’ is always from 1 to 31, the value of ‘m’ is from 1 to 12, we can optimize
the memory using bit fields like the code below:

#include <stdio.h>
// A simple representation of the date
struct date {
unsigned int d : 5;
unsigned int m : 5;
unsigned int y;
};
int main()
{
printf("Size of date is %lu bytes\n", sizeof(struct date));
struct date dt = { 31, 12, 2014 };
printf("Date is %d/%d/%d", dt.d, dt.m, dt.y);
}

Output:
Size of date is 8 bytes
Date is 31/12/2014
In the above code the structure requires 8 bytes of memory space for ‘dt’ variable, but only 5 bits will be
used to store the values for ‘d’ and ‘m’ and 4 bytes for ‘y’.

Properties of bit fields:

1. A special unnamed bit field of size 0 is used to force alignment on next boundary.
2. We cannot have pointers to bit field members as they may not start at a byte boundary.
3. We can have static members in a structure/class, but bit fields cannot be static.
4. Array of bit fields is not allowed. For example, the below program fails in the compilation.

You might also like