Exercises — Bit fields in structs
5.1.23 · D4· Coding › C Programming › Bit fields in structs
Parent: Bit fields in structs. Yeh page ek self-testing ladder hai. Har problem apna question clearly state karta hai; Solution ek collapsible callout ke andar chhupa hua hai taaki tum pehle khud try kar sako, phir reveal karo. Levels recognising the syntax se mastering real hardware-register layouts tak jaate hain.
Shuru karne se pehle, ek shared picture jo hamesha kaam aayegi. Ek bit field ek storage unit ke andar rehta hai — unsigned int ko numbered slots ki ek row ki tarah socho, jo in figures mein fields se bhari hoti hai. Har packing question ke liye yeh baat dhyan mein rakho.

Vault prerequisites recall karo agar koi word naya lage: Data types and sizeof, Bitwise operators, Unsigned vs signed integers, Memory alignment and padding, Structs in C.
Level 1 — Recognition
Recall Solution — L1·Q1
Answer: (b).
- (a) illegal — bit field ka
typestandard ke allowed integer types mein se ek hona chahiye (signed int,unsigned int,_Bool).floatnahi hai. - (c) illegal — width ek non-negative integer constant hona chahiye;
2.5integer nahi hai. - (d) illegal — bit field ek single scalar hota hai, tum array ko bit field nahi bana sakte.
- (b) legal —
type member : width;ek allowed integer type aur integer width ke saath. ✓
Recall Solution — L1·Q2
Counting rule use karo: bits combinations dete hain, sabse bada all-ones hota hai. Sabse bada = 15.
Level 2 — Application
Recall Solution — L2·Q1
Yeh step kyun: humein chahiye , yaani .
- ✓
Answer: bits. Declaration: unsigned int val : 10; ( rakhta hai).
Recall Solution — L2·Q2
Ek 3-bit field sirf low 3 bits rakhta hai = value mod .
4 print hoga. Koi error nahi, koi warning nahi — silent truncation. Figure s02 dekho: 20 ke top bits simply 3-slot box ke left edge se gir jaate hain.

Recall Solution — L2·Q3
Ek signed field apna top bit sign par kharch karta hai (dekho Unsigned vs signed integers). bits mein two's complement ke saath: Answer: −8 to 7 (kul 16 values, lekin sirf 0..7 non-negative hain).
Level 3 — Analysis
Recall Solution — L3·Q1
Ek 32-bit unit mein offset track karo (figure s03):
abits 0–4 leta hai (offset ab 5 hai).bbits 5–9 leta hai (offset ab 10 hai).cko 25 bits chahiye, lekin sirf bacha hai → fit nahi hoga. Yeh ek fresh unit se shuru hota hai (unit #2 ke bits 0–24).
Do storage units use ho rahe hain → bytes.
sizeof(struct S) = 8 in assumptions ke saath (32-bit unit, left-to-right packing). Alag unit size ya packing direction wale compiler par number alag ho sakta hai — reasoning, constant nahi, yahi important hai.

Recall Solution — L3·Q2
unsigned : 0; align-to-next-unit instruction hai: yeh force karta hai ki uske baad wala field ek fresh storage unit se shuru ho.
low→ unit #1 ke bits 0–3.: 0→ unit #1 band karo, unit #2 par jump karo.high→ unit #2 ke bits 0–3.
Do units → sizeof(struct R) = 8 bytes (4-byte units assume karte hue). (Contrast: bina : 0 divider ke, dono 4-bit fields ek unit mein fit ho jaate → 4 bytes.) Dekho Memory alignment and padding.
Recall Solution — L3·Q3
Yeh compile NAHI hoga. Sabse chhoti cheez jo ek C pointer address kar sakta hai woh ek byte hai. Ek 1-bit field ek byte ke beech mein ho sakta hai (jaise byte 0 ka bit 3), jiska khud ka koi byte address nahi hota. Toh &s.flag ek compile error hai.
Fix — pehle ek normal variable mein copy karo:
unsigned t = s.flag; // t ek poori addressable variable hai
unsigned *p = &t; // legalLevel 4 — Synthesis
Recall Solution — L4·Q1
Widths add karo: bits → logical layout ko exactly ek byte chahiye. Portable standard C mein sirf allowed bit-field types unsigned int / signed int / _Bool hain, toh hum unsigned int se declare karte hain (dekho Embedded systems / hardware registers):
struct StatusReg {
unsigned int ready : 1; // bit 0
unsigned int speed : 3; // bits 1..3
unsigned int error : 2; // bits 4..5
unsigned int reserved : 2; // bits 6..7
};Sab 8 logical bits account ho gaye; speed theek se rakhta hai. ✓
sizeof ke baare mein honest caveat: kyunki storage unit ek unsigned int hai, sizeof(struct StatusReg) typically 4 bytes hoga, 1 nahi — fields ek 4-byte unit ke low 8 bits mein rehti hain, baaki padding hai. Tum portably ek 1-byte struct force nahi kar sakte: unsigned char ready : 1; ek compiler extension hai (GCC/Clang accept karta hai, lekin standard char ko bit-field type list mein nahi rakhta, aur tab bhi kuch targets par yeh ek int mein pack ho sakta hai). Agar tumhe sach mein ek exact 1-byte hardware image chahiye, toh portable route hai raw unsigned char plus manual shift-and-mask (agla exercise), ya compiler-specific __attribute__((packed)).
Recall Solution — L4·Q2
speed bits 1–3 par baitha hai. Ise neeche shift karo taaki bit 1 position 0 par aa jaye, phir exactly 3 bits rakhne ke liye mask karo.
- 1 se right shift:
reg >> 1bits 1–3 ko positions 0–2 par le aata hai. - se mask:
& 0x7exactly woh 3 bits rakhta hai.
unsigned speed = (reg >> 1) & 0x7;reg = 0b01011010 (= 90) se check karo: reg >> 1 = 0b00101101, & 0b111 = 0b101 = 5. Toh speed = 5. Yeh shift-and-mask fully portable hai — yeh packing direction, unit size, ya signedness par depend nahi karta jis tarah bit-field layout karta hai.
Level 5 — Mastery
Recall Solution — L5·Q1
Har field 4 bits hai → value mod rakho.
s.a = 25: . (Binary: , low 4 bits .)s.b = 7: , as-is store hoga .
Output: 9 7. Dono fields ek hi storage unit mein rehti hain (4+4 = 8 bits ≤ 32), ek doosre se independent. (9 aur 7 values portable hain; kaun se physical bits unhe rakhte hain yeh packing direction par depend karta hai.)
Recall Solution — L5·Q2
Har field ko uski starting bit par place karo aur add karo (yeh packing hai, haath se ki gayi — order problem ne fix kiya hai, toh ambiguity nahi):
ready(bit 0):speed(bits 1–3):error(bits 4–5):reserved(bits 6–7):
Answer: 43. Right-to-left padhne par: reserved=00, error=10, speed=101, ready=1 — inputs se match karta hai. ✓
Recall Solution — L5·Q3
p→ unit #1 ke bits 0–9 (offset 10).q→ unit #1 ke bits 10–19 (offset 20).: 0→ unit #1 band karo, next field ko fresh unit par force karo.r→ unit #2 ke bits 0–9.
Haalaanki r (10 bits) unit #1 ke remaining 12 bits mein fit ho jaata, zero-width field ne use unit #2 par push kar diya. Do units use hue → sizeof(struct M) = 8 bytes (32-bit-unit assumption ke under).
Recall Rapid recap — woh char moves jo tumne practise kiye
Truncation ::: value stored = assigned value mod (low N bits rakho).
Signed range ::: to ; unsigned range to .
Packing rule ::: storage unit ko tab tak bharo jab tak ek field fit na ho; phir fresh unit shuru karo.
Bit k par field extract karo ::: (reg >> k) & mask — pehle shift, phir mask.
Kaun se parts portable hain ::: values/widths/wrapping hain; exact byte layout (packing order, unit size) implementation-defined hai.
Connections
- Parent: Bit fields in structs
- Bitwise operators — har field ke peeche manual shift-and-mask.
- Data types and sizeof — kyun unit ek
inthai aursizeofchunks mein jump karta hai. - Memory alignment and padding — zero-width divider action mein.
- Embedded systems / hardware registers — L4/L5 status-register pattern.
- Unsigned vs signed integers — L2·Q3 mein kho gaya sign bit.
- Structs in C — bit fields sirf special struct members hain.