Bit Fields in C Programming with Examples

What are Bit Fields in C?

A bit field in C allows the allocation of a specific number of bits to structure members, enabling memory-efficient storage for data that requires only a few bits, such as flags, status indicators, or small counters.

Used mainly when:

  • Space is critical (like in embedded systems)
  • A variable doesn’t need full byte or word storage
  • You want precise control over layout

Why Use Bit Fields?

Consider this: If you want to store several flags (ON/OFF) or small-range integers (0–3), using a whole int (usually 4 bytes) for each of them is wasteful. Bit fields allow you to:

  • Store multiple flags within a single byte or integer.
  • Control memory layout tightly.
  • Reduce memory usage.

Declaring Bit Fields in C

Bit fields allow you to define structure members with a fixed number of bits, making them ideal for memory-efficient flags or status codes.

Syntax:

struct StructName {
    data_type member_name : bit_size;
};
  • data_type: Usually int, unsigned int, or signed int.
  • bit_size: Number of bits to allocate for that field.

Example:

advertisement
struct Access {
    unsigned int canRead      : 1;  // 1 bit
    unsigned int canWrite     : 1;  // 1 bit
    unsigned int canExecute   : 1;  // 1 bit
    unsigned int priorityLevel : 2; // 2 bits (range 0 to 3)
};

Accessing Bit Fields in C

Accessing and modifying bit fields is similar to regular structure members.​

Free 30-Day C Certification Bootcamp is Live. Join Now!
struct Access user;
 
user.canRead = 1;
user.canWrite = 0;
user.canExecute = 1;
user.priorityLevel = 2;
 
printf("Read Access    : %u\n", user.canRead);
printf("Write Access   : %u\n", user.canWrite);
printf("Execute Access : %u\n", user.canExecute);
printf("Priority Level : %u\n", user.priorityLevel);

Note: You cannot use the address-of operator (&) with bit fields.

Example of Bit Fields in C

#include <stdio.h>
 
struct Access {
    unsigned int canRead      : 1;
    unsigned int canWrite     : 1;
    unsigned int canExecute   : 1;
    unsigned int priorityLevel : 2;
};
 
int main() {
    struct Access user;
 
    user.canRead = 1;
    user.canWrite = 0;
    user.canExecute = 1;
    user.priorityLevel = 2;
 
    printf("Read Access    : %u\n", user.canRead);
    printf("Write Access   : %u\n", user.canWrite);
    printf("Execute Access : %u\n", user.canExecute);
    printf("Priority Level : %u\n", user.priorityLevel);
    printf("Struct Size    : %lu bytes\n", sizeof(user));
 
    return 0;
}

Output:

Read Access    : 1  
Write Access   : 0  
Execute Access : 1  
Priority Level : 2  
Struct Size    : 4 bytes

This program defines a structure with bit fields to store access rights and priority level using fewer bits. Each field takes only the number of bits it needs 1 bit for read, write, and execute, and 2 bits for priority. In the main() function, it sets the values and prints them, along with the total size of the structure. Bit fields are useful when you want to save memory by storing small values more efficiently.

Limitations of Bit Fields

  • Portability Issues: The order of bits (endianness) and the use of padding can differ between compilers and systems, which makes bit fields less portable.
  • No Addressability: You cannot take the address of a bit field member using the address-of operator (&).
  • Limited to Integers: Bit fields only work with integer types, like int or unsigned int.
  • Performance Overhead: Accessing bit fields may require extra masking and shifting operations, which can affect performance.​

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.