Reading history

Bit Fields in C: Packing Struct Members into Bits

4 min read 2 views

Learn bit fields in C: declaring members with a bit width, saving memory, one-bit flags, truncation of large values, and the limitations and portability of bit fields.

A bit field is a structure member that occupies a chosen number of bits instead of a whole byte or word. If a value only ever ranges from 1 to 31, such as a day of the month, it needs just 5 bits. Bit fields let C pack several small values into one integer, saving memory in large arrays, network packets and hardware registers, while still accessing them with ordinary member syntax.

In this lesson you will learn

  • Declaring members with : width
  • How much memory bit fields save
  • One-bit flags
  • What happens when a value does not fit
  • Limitations and portability of bit fields

This lesson builds on Structures in C.

Packing a Date and Flags

#include <stdio.h>

struct DateNormal {
    unsigned int day;
    unsigned int month;
    unsigned int year;
};

struct DatePacked {
    unsigned int day   : 5;    /* 0..31   */
    unsigned int month : 4;    /* 0..15   */
    unsigned int year  : 12;   /* 0..4095 */
};

struct Flags {
    unsigned int is_admin  : 1;
    unsigned int is_active : 1;
    unsigned int level     : 3;   /* 0..7 */
};

int main(void) {
    struct DatePacked d = {16, 11, 2026};
    printf("date: %u/%u/%u\n", d.day, d.month, d.year);
    printf("normal: %zu bytes, packed: %zu bytes\n",
           sizeof(struct DateNormal), sizeof(struct DatePacked));

    struct Flags f = {0};
    f.is_active = 1;
    f.level = 5;
    printf("admin=%u active=%u level=%u size=%zu\n", f.is_admin, f.is_active, f.level, sizeof f);

    unsigned int big = 9;
    f.level = big & 7;            /* only 3 bits fit: 9 (1001) keeps 001 */
    printf("level after storing 9: %u\n", f.level);
    return 0;
}

Output

date: 16/11/2026
normal: 12 bytes, packed: 4 bytes
admin=0 active=1 level=5 size=4
level after storing 9: 1
  • unsigned int day : 5; gives day 5 bits, enough for 0 to 31. Month needs 4 bits (0 to 15) and year 12 bits (0 to 4095). Together they need 21 bits, which fit in one 4-byte unit, so the packed date takes 4 bytes instead of 12.
  • One-bit fields such as is_admin : 1 are natural on/off flags, a readable alternative to the masks in Bitwise Operators.
  • Bit fields are read and written like normal members: f.level = 5;.
  • A value that is too large is reduced modulo 2width for unsigned fields: storing 9 (binary 1001) in a 3-bit field keeps only 001. The example masks explicitly to show the effect; assigning 9 directly would give the same result plus a compiler warning.
  • Use unsigned int (or _Bool/signed int) as the base type. Whether a plain int bit field is signed is implementation-defined.

Bits Needed for Common Ranges

ValuesBitsExample
0 to 11Flag
0 to 73Day of week
0 to 154Month
0 to 315Day of month
0 to 636Minutes or seconds
0 to 2558Colour channel

Limitations

  • You cannot take the address of a bit field (&d.day is an error), so scanf cannot read into one directly; read into a normal variable and assign it.
  • Arrays of bit fields are not allowed.
  • The order of bits within a unit, and whether fields may cross unit boundaries, depend on the compiler. Do not use bit fields to match an exact external file or network format; use shifts and masks for that.
  • Accessing a bit field can be slightly slower than a whole integer, because the compiler must mask and shift. The trade is memory for a little speed.

Common Mistakes

  1. Choosing a width that is too small, silently losing data.
  2. Using signed bit fields of width 1, which can only hold 0 and -1.
  3. Assuming a particular layout when writing the struct to a file or socket.
  4. Expecting sizeof to equal the total bits; the size is rounded up to whole storage units.

Practice Exercises

Exercise 1. Design a bit-field struct for a time of day: hours (0 to 23), minutes and seconds (0 to 59).

Show answer
struct Time {
    unsigned int hour   : 5;   /* 0..31 */
    unsigned int minute : 6;   /* 0..63 */
    unsigned int second : 6;   /* 0..63 */
};   /* 17 bits, typically 4 bytes */

Exercise 2. Why does scanf("%u", &t.hour); not compile, and what should you write instead?

Show answer

Bit fields have no address. Read into a temporary: unsigned h; scanf("%u", &h); t.hour = h;

Frequently Asked Questions

What does an unnamed bit field like “unsigned : 3;” do?

It reserves 3 bits of padding. A zero-width unnamed field, unsigned : 0;, forces the next field to start in a new storage unit.

Bit fields or bit masks?

Bit fields are more readable for data that stays inside your program. Masks and shifts give exact, portable control over bit positions, which you need for file formats, protocols and registers.

Summary

  • type name : width; makes a member use only width bits.
  • Bit fields pack small values together to save memory.
  • Values that do not fit are truncated.
  • Their layout is compiler-specific, and they have no address.

Next: Structure Padding in C.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *