C Programming • Structure and Union
C Programming / Bit-fields

Bit-fields

Notes 4 Structure and Union

A bit-field is a special feature of C structures that allows a member to use a specified number of bits instead of using the normal size of its data type. Bit-fields are useful when a variable needs to store only a small range of values.

Notes

A bit-field is a special feature of C structures that allows a member to use a specified number of bits instead of using the normal size of its data type. Bit-fields are useful when a variable needs to store only a small range of values.

💡 Example
unsigned int gender : 1;

A 1-bit field can represent two states, such as 0 and 1.

Why Are Bit-fields Used?

If a variable needs only a few possible values, using a complete integer for it may use more memory than necessary. Bit-fields allow the programmer to specify only the number of bits required for that particular member.

💡 Example

If a variable can have only two values, 0 and 1, one bit is sufficient to represent it.

Number of Values and Required Bits

The number of bits determines how many different values can be represented. With n bits, up to 2n different combinations can be represented.

Bits Possible Values
1 bit 2 values
2 bits 4 values
3 bits 8 values
4 bits 16 values
💡 Example

If a value can range from 0 to 3, four different values are possible, so 2 bits are sufficient.

Syntax of a Bit-field

A bit-field is declared inside a structure by writing the data type, member name, and the number of bits after a colon.

data_type member_name : number_of_bits;
💡 Example
unsigned int gender : 1; unsigned int status : 2;

Bit-fields Inside a Structure

Bit-fields are generally used as members of a structure. Different members can be assigned different numbers of bits according to the values they need to represent.

💡 Example
struct Employee { unsigned int gender : 1; unsigned int status : 2; unsigned int hobby : 3; };

Here, each member is given a specific number of bits according to the amount of information it needs to store.

Important Points About Bit-fields

  • Bit-fields are declared inside a structure.
  • The width of a bit-field is specified after a colon.
  • The width indicates how many bits are allocated to that member.
  • Bit-fields are useful when memory usage needs to be reduced.
  • The values that can be represented depend on the number of bits assigned to the field.

Example from the Concept

Suppose an employee record needs to store whether the employee is male or female, the marital status, a hobby category, and a scheme number. These fields can be represented using different numbers of bits instead of using separate full-size integer members.

💡 Example
unsigned int gender : 1; unsigned int mar_status : 2; unsigned int hobby : 3; unsigned int scheme : 4;

The number after : specifies the number of bits assigned to each member.

Practical Example

Problem Statement

Write a C program using bit-fields to store simple employee information such as gender, marital status and hobby, and display the stored values.

Learning Outcomes

  • Understand the purpose of bit-fields.
  • Declare bit-fields inside a structure.
  • Assign and access values stored in bit-fields.

Hint

Define an Employee structure with bit-fields for gender, marital status and hobby. Assign values and display them.

Theory

Bit-fields allow a structure member to occupy a specified number of bits. This is useful for data whose possible values are limited, because only the required number of bits needs to be allocated.

Program

#include <stdio.h> struct Employee { unsigned int gender : 1; unsigned int maritalStatus : 2; unsigned int hobby : 3; }; int main() { struct Employee e1; e1.gender = 1; e1.maritalStatus = 2; e1.hobby = 5; // display the values stored in bit-fields printf("Gender = %u ", e1.gender); printf("Marital Status = %u ", e1.maritalStatus); printf("Hobby = %u ", e1.hobby); return 0; }

Expected Output

Gender = 1

Marital Status = 2

Hobby = 5

Note

In the program, gender uses 1 bit, maritalStatus uses 2 bits, and hobby uses 3 bits. The bit-field width limits the range of values that can be stored in each member.

Advantages of Bit-fields

Advantage Explanation
Memory Efficiency Only the specified number of bits is allocated to a field.
Compact Data Useful for storing several small values together.
Useful for Flags Suitable for values that represent ON/OFF or similar states.
Structured Representation Related bit-level information can be grouped in a structure.

Limitations

Bit-fields are useful for compact data storage, but their exact memory layout and some implementation details can depend on the compiler. Therefore, they should be used carefully when a program must depend on a particular memory representation.

⚠️ Remember

Bit-fields are mainly useful when the range of possible values is known and small.

Quick Revision

Concept Remember
Bit-field Structure member with a specified number of bits.
Syntax type member : width;
1 Bit Can represent 2 possible combinations.
2 Bits Can represent 4 possible combinations.
Use Useful for compact storage of small-range values.

Important Exam Questions

Short Answer Questions

  1. What is a bit-field in C?
  2. Why are bit-fields used?
  3. Write the syntax of a bit-field.
  4. How many different combinations can be represented using 3 bits?
  5. Where are bit-fields declared?
  6. What does the number after the colon represent?

Long Answer Questions

  1. Explain bit-fields in C with syntax and suitable example.
  2. Explain the advantages of using bit-fields in structures.
  3. Write a C program to demonstrate the use of bit-fields.
  4. Explain how the number of bits affects the values that can be stored in a bit-field.
🎥 Recommended Learning

Watch a beginner-friendly explanation of bit-fields in C.

▶ Watch: Bit-fields in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for bit-fields will be provided here.

🧠 Mind Map

Use the mind map for quick revision of bit-field width, structure declaration and memory efficiency.