C Bit-fields

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Bit-fields?

A bit-field is a struct member with a declared width in bits: unsigned mode : 3; holds values 0 to 7. Bit-fields save memory for flags and tightly packed data, but their layout, order, and padding are implementation-defined, so avoid them for file formats and network protocols.

Remember
struct Flags {
    unsigned ready : 1;   /* 0..1  */
    unsigned mode  : 3;   /* 0..7  */
    unsigned level : 4;   /* 0..15 */
};

Cheat Sheet

Keep these facts handy while you work with Bit-fields in C.

TopicDetail
Declaretype name : width;
Use unsignedSignedness of plain int bit-fields is implementation-defined
No addressCannot use & on a bit-field
Portable alternativeMasks and shifts

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Dynamic Memory Allocation.

Example 1 — Packing three values

Each field stores only its allowed range.

C
#include <stdio.h>

struct Flags {
    unsigned ready : 1;
    unsigned mode  : 3;
    unsigned level : 4;
};

int main(void) {
    struct Flags f = {1, 5, 9};

    printf("ready=%u mode=%u level=%u\n", f.ready, f.mode, f.level);
    f.mode = 7;
    printf("mode=%u\n", f.mode);
    printf("sizeof Flags = %zu\n", sizeof f);
    return 0;
}

How It Works

  1. The three fields use 1 + 3 + 4 = 8 bits.
  2. GCC and Clang store them in one unsigned unit, so the size is typically 4 bytes.
  3. Assigning a value too large for the width wraps or is implementation-defined, so keep to the range.

Edge Cases & Pitfalls

Check these before relying on Bit-fields in real programs.

layout

Non-portable layout

Bit order and packing differ across compilers. Use explicit masks for hardware registers and protocols.

addr

Taking an address

&f.mode does not compile.

Key Takeaways

  • Width: name : bits.
  • Unsigned: avoids surprises.
  • Layout: not portable.

One line: A bit-field is a struct member with a declared width in bits: unsigned mode : 3; holds values 0 to 7.

Frequently Asked Questions

Bit-fields store small integers or flags in fewer bits than a full int, which is useful for compact data and hardware register maps in embedded code.
Bit-fields are more readable, but bitmasks and shifts give you exact, portable control over layout.
Continue with "C Dynamic Memory Allocation" next. Allocate and release memory at run time with malloc, calloc, realloc, and free, and avoid leaks and dangling pointers. The C Structures, Unions and Enums hub lists every topic in order.

Next: Dynamic Memory Allocation

Allocate and release memory at run time with malloc, calloc, realloc, and free, and avoid leaks and dangling pointers.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful