Skip to content
StudyFame

Bit Fields in C

A date needs a day (1–31), a month (1–12) and a year. Storing each in an int uses 12 bytes. But a day fits in just 5 bits and a month in 4 bits! Bit fields let a structure member use only as many bits as it needs.

In this lesson you will declare bit fields, measure the memory they save, see what happens when a value is too big, and learn their rules and limitations. They are widely used in embedded systems, device drivers and network packets.

What You Will Learn

  • What a bit field is.
  • How to declare bit fields in a structure.
  • How many values n bits can hold.
  • How much memory bit fields save.
  • What happens on overflow.
  • Rules and limitations of bit fields.

What are Bit Fields in C?

Exam definition: A bit field is a structure (or union) member declared with a specific width in bits, using the syntax type name : width;. Bit fields let several small values be packed into one storage unit to save memory.

Memory trick: Bit fields are like packing a suitcase smartly. Instead of putting each sock in its own big box (a whole int), you roll them up and fit many into one box. n bits hold 2ⁿ values: 1 bit = 2 (yes/no), 4 bits = 16, 5 bits = 32.

Syntax of Bit Fields

struct date
{
    unsigned int day   : 5;   /* 0 to 31   (2^5 = 32 values)  */
    unsigned int month : 4;   /* 0 to 15   (2^4 = 16 values)  */
    unsigned int year  : 12;  /* 0 to 4095 (2^12 = 4096 values) */
};
BitsUnsigned rangeGood for
10 – 1On/off flags
30 – 7Day of week
40 – 15Month (1–12)
50 – 31Day of month (1–31)
70 – 127Marks out of 100
  • Use unsigned int (or signed int, _Bool). Plain int bit fields may be signed or unsigned depending on the compiler, so be explicit.
  • The width can't be more than the bits in the type (32 for int).
  • Range for n unsigned bits: 0 to 2ⁿ − 1.

Example 1: Packing a Date into 4 Bytes

#include <stdio.h>

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

struct date_packed
{
    unsigned int day   : 5;
    unsigned int month : 4;
    unsigned int year  : 12;
};

int main(void)
{
    struct date_packed d = {26, 1, 1950};

    printf("Republic Day: %02u-%02u-%u\n", d.day, d.month, d.year);
    printf("sizeof(date_normal) = %zu bytes\n", sizeof(struct date_normal));
    printf("sizeof(date_packed) = %zu bytes\n", sizeof(struct date_packed));
    printf("Bits actually used  = %d of 32\n", 5 + 4 + 12);

    d.day = 15;                     /* use like normal members */
    d.month = 8;
    d.year = 1947;
    printf("Independence Day: %02u-%02u-%u\n", d.day, d.month, d.year);

    return 0;
}

Output

Republic Day: 26-01-1950
sizeof(date_normal) = 12 bytes
sizeof(date_packed) = 4 bytes
Bits actually used  = 21 of 32
Independence Day: 15-08-1947

Code Explanation

  1. day : 5, month : 4 and year : 12 need 21 bits, which fit inside one 32-bit unit.
  2. So the packed date takes 4 bytes instead of 12, saving two-thirds of the memory.
  3. Bit fields are read and written like normal members: d.day = 15;.
  4. %u prints them as unsigned values.

How Bit Fields Overflow

A bit field can't hold a value bigger than its bits allow. Extra high bits are cut off; for unsigned fields, the stored value is value mod 2ⁿ.

unsigned int month : 4;       range 0..15

month = 12  -> binary  1100        -> stored 12  (fits)
month = 18  -> binary 1 0010       -> top bit lost -> 0010 = 2   (18 mod 16)

The compiler may warn about this, but the program still runs with the wrong value. Always choose enough bits for the biggest value.

Example 2: Status Flags and Overflow

#include <stdio.h>

struct device_status
{
    unsigned int power   : 1;    /* 0 = off, 1 = on */
    unsigned int wifi    : 1;
    unsigned int battery : 7;    /* 0..127, we use 0..100 */
    unsigned int mode    : 2;    /* 0..3 */
};

struct small
{
    unsigned int month : 4;      /* 0..15 */
};

int main(void)
{
    struct device_status phone = {1, 0, 85, 2};
    struct small s;
    unsigned int value = 18;

    printf("Power  : %s\n", phone.power ? "ON" : "OFF");
    printf("Wi-Fi  : %s\n", phone.wifi ? "ON" : "OFF");
    printf("Battery: %u%%\n", phone.battery);
    printf("Mode   : %u\n", phone.mode);
    printf("Size   : %zu bytes for 4 settings\n", sizeof(phone));

    phone.wifi = 1;                       /* toggle a flag */
    printf("Wi-Fi after toggle: %s\n", phone.wifi ? "ON" : "OFF");

    s.month = value;                      /* 18 doesn't fit in 4 bits */
    printf("Stored 18 in 4 bits -> %u\n", s.month);

    return 0;
}

Output

Power  : ON
Wi-Fi  : OFF
Battery: 85%
Mode   : 2
Size   : 4 bytes for 4 settings
Wi-Fi after toggle: ON
Stored 18 in 4 bits -> 2
  • Four settings fit in 11 bits, inside one 4-byte unit. Four separate ints would take 16 bytes.
  • 1-bit fields are perfect for on/off flags.
  • Storing 18 in a 4-bit field keeps only the low 4 bits: 18 mod 16 = 2.

Rules and Limitations of Bit Fields

  • You cannot take the address of a bit field: &d.day is an error. So scanf("%u", &d.day) doesn't work; read into a normal variable, then assign.
  • You cannot make an array of bit fields, and sizeof can't be applied to one bit field.
  • The width can't exceed the type's size (e.g. 32 for unsigned int).
  • How bits are arranged inside the unit (left to right or right to left) is compiler-dependent, so don't rely on the layout for files shared between systems.
  • Accessing bit fields can be a little slower than plain ints (extra shifting and masking).
  • A zero-width unnamed field (unsigned int : 0;) forces the next field to start in a new unit.

Important Points to Remember

  • Syntax: unsigned int name : width;
  • n bits hold 2ⁿ values: range 0 to 2ⁿ − 1.
  • Bit fields save memory by packing small values together.
  • Overflow keeps only the low bits (value mod 2ⁿ).
  • No &, no arrays of bit fields, no sizeof on a field.
  • Used in embedded systems, drivers, network headers.

Common Mistakes Beginners Make

MistakeWhat happensFix
scanf("%u", &d.day)Error: can't take address of bit fieldRead into a temp variable, then assign
Too few bits (month : 3)Months 8–12 stored wronglyUse 4 bits (0–15)
Plain int x : 1; to store 1May read back as −1 (signed)Use unsigned int
Width larger than the typeCompile errorMax 32 for unsigned int
Writing bit-field structs to files for other systemsLayout may differUse explicit shifts and masks

Bit Fields Practice Questions

  1. Define a bit field and write its syntax.
  2. How many bits are needed to store hours (0–23)? Minutes (0–59)?
  3. What is stored if you assign 20 to a 4-bit unsigned bit field?
  4. Why can't scanf read directly into a bit field?
  5. Give two uses of bit fields.
Show answers
  1. A structure member with a fixed width in bits: type name : width;
  2. Hours: 5 bits (0–31). Minutes: 6 bits (0–63).
  3. 4 (20 mod 16).
  4. scanf needs an address, and a bit field has no address of its own.
  5. Storing on/off flags compactly; packing dates/times; hardware registers and network packet headers.

Coding Exercise

Create a bit-field structure time with hours (5 bits), minutes (6 bits) and seconds (6 bits). Store 23:59:45, print it, and print the structure's size.

Show solution
#include <stdio.h>

struct time
{
    unsigned int h : 5;
    unsigned int m : 6;
    unsigned int s : 6;
};

int main(void)
{
    struct time t = {23, 59, 45};
    printf("%02u:%02u:%02u\n", t.h, t.m, t.s);
    printf("Size: %zu bytes\n", sizeof(t));
    return 0;
}
23:59:45
Size: 4 bytes

Quick Quiz: Bit Fields (MCQ)

  1. Correct bit field declaration:

    A) unsigned int a[3];   B) unsigned int a : 3;   C) unsigned int 3 : a;   D) bit a = 3;

  2. Range of a 3-bit unsigned bit field:

    A) 0–3   B) 0–7   C) 0–8   D) 1–8

  3. Which is not allowed with bit fields?

    A) Assigning a value   B) Printing the value   C) Taking the address with &   D) Using in an expression

  4. Main purpose of bit fields:

    A) Faster loops   B) Saving memory   C) Bigger numbers   D) String handling

  5. Assigning 9 to a 3-bit unsigned field stores:

    A) 9   B) 1   C) 7   D) 0

Show answers
  1. B: unsigned int a : 3;
  2. B: 0–7.
  3. C: taking the address.
  4. B: saving memory.
  5. B: 1 (9 mod 8).

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Bit fieldMember with a fixed number of bits
Syntaxunsigned int name : width;
n bits2ⁿ values, range 0 to 2ⁿ − 1
PurposeSave memory by packing
OverflowKeeps low bits (value mod 2ⁿ)
Not allowed&field, arrays of fields, sizeof(field)

Frequently asked questions

What are bit fields in C?
Structure members declared with a width in bits (: n) so small values can be packed to save memory.
What are the limitations of bit fields?
No address (&), no arrays, width ≤ type size, and the bit layout is compiler-dependent.
Where are bit fields used?
In embedded systems, device registers, flags and network protocol headers.

Remember

  • Bit fields = smart suitcase packing.
  • Count bits: 2ⁿ must cover the biggest value.

Next Topic

Next lesson: Dynamic Memory Allocation in C. You will start Module 12 and request memory at run time with malloc, calloc, realloc and free.