Bit fields in structs
Topic: Packing data into individual bits inside a struct so each member uses only the number of bits you ask for, not a whole byte/int.
The Big Picture
struct Flags {
unsigned int isActive : 1; // 1 bit -> stores 0 or 1
unsigned int level : 3; // 3 bits -> stores 0..7
unsigned int mode : 4; // 4 bits -> stores 0..15
};WHY do bit fields exist?
- WHAT problem: memory and hardware registers. A device status register might be one 8-bit byte where bit 0 = "ready", bits 1–3 = "speed", etc.
- WHY useful: you save memory when storing many small values (network packet headers, embedded systems, file formats) and you can mirror hardware bit layouts directly in C.
- HOW it works: the compiler tracks a "current storage unit" and an offset; each new bit field is placed at the next free bits, advancing the offset. When a field won't fit (or you force it), a new unit starts.
How many values fit in N bits?
Memory layout and packing

struct S {
unsigned a : 5; // boxes 0..4
unsigned b : 5; // boxes 5..9
unsigned c : 25; // would need boxes 10..34 -> won't fit!
}; // c starts a NEW int => struct is 8 bytesThe zero-width trick
struct Reg {
unsigned low : 4;
unsigned : 0; // align to next unit
unsigned high : 4; // starts in a new int
};Rules & Restrictions (the gotchas)
Forecast-then-Verify
Flashcards
What syntax declares a 4-bit unsigned struct member named x?
unsigned int x : 4;How many distinct values does an N-bit unsigned bit field hold?
Why is &s.flag illegal for a bit field flag?
What happens when you assign 9 to a 3-bit field?
What does an unnamed zero-width field unsigned : 0; do?
Why might sizeof of a struct with three 1-bit fields be 4, not 1?
What types are allowed for bit fields?
int, signed int, unsigned int, and _Bool (C99).Range of a signed 3-bit field?
Is bit-field allocation order (low→high vs high→low) defined by the standard?
Recall Feynman: explain to a 12-year-old
Imagine a long shelf with 32 tiny slots. Normally if you want to store one yes/no answer, you waste a whole separate shelf of 32 slots on it. Bit fields let you say "this answer only needs 1 slot," so you can fit lots of little answers on one shelf. The catch: because the answer is jammed in a tiny slot and not in its own labeled box, you can't point at it and say "that's its exact address" — and if you try to store a number too big for its slots, the extra digits just fall off the edge.
Connections
- Structs in C — bit fields are a special kind of struct member.
- Bitwise operators —
& | ^ << >>are the manual alternative to bit fields. - Data types and sizeof — why an int is the storage unit.
- Memory alignment and padding — the zero-width field controls alignment.
- Embedded systems / hardware registers — the main real-world use case.
- Unsigned vs signed integers — explains the lost sign bit.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Bit fields ka idea simple hai: normally ek int member chahe usme tum sirf 0/1 store karo, woh poora 4 byte khaa jaata hai. Bit field se tum bol dete ho ki "bhai ye member sirf 3 bit ka hai" — unsigned int level : 3;. Phir compiler in chhote-chhote members ko ek hi int ke andar paas-paas thoonk deta hai, jaise ek hi shelf par kai chhoti cheezein adjust kar di. Isse memory bachti hai, especially embedded systems aur network packet headers mein jahan har bit count karta hai.
Kitni values aayengi? N bit mein values, yaani range 0 se . Month 1–12 store karna hai to , matlab 4 bit chahiye. Yaad rakho: agar tum field ki capacity se bada number daaloge (jaise 3-bit field mein 9), to error nahi aayega — bas upar ke bits gir jaayenge, reh jaayega. Ye silent truncation hai, khud check karna padta hai.
Do bade gotchas: (1) &s.flag allowed nahi hai, kyunki bit field byte boundary par start nahi hota, uska koi proper address nahi hota — pehle normal variable mein copy karo. (2) Signed field (int x:3) mein top bit sign ke liye chala jaata hai, to range −4 se 3 ho jaati hai, 0..7 nahi. Isliye hamesha unsigned likho jab 0 se positive range chahiye. Aur unsigned : 0; ek trick hai jo agle field ko naye storage unit se shuru karwa deta hai (alignment ke liye).