Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 3 — Bit Fields and Miscellaneous

Lesson 30 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Bit Fields in C Structures

A bit field is a member of a structure that occupies a specified number of bits rather than full bytes. Bit fields are used to save memory when storing small integer values.

---

Defining Bit Fields

struct Flags {
    unsigned int isActive  : 1;  /* 1 bit — 0 or 1 */
    unsigned int isAdmin   : 1;  /* 1 bit */
    unsigned int priority  : 3;  /* 3 bits — 0 to 7 */
    unsigned int reserved  : 3;  /* 3 bits padding */
};
struct Flags f;
f.isActive = 1;
f.isAdmin  = 0;
f.priority = 5;

printf("Active: %d, Priority: %d\n", f.isActive, f.priority);

---

Bit Field Rules

  • Type must be int, unsigned int, or signed int (C99 also allows _Bool)
  • Width must be between 0 and the width of the type (0 = padding to next boundary)
  • Cannot take address of a bit field (& not allowed)
  • Cannot create arrays of bit fields

---

Memory Savings Example

struct WithoutBitField {
    int isActive;  /* 4 bytes */
    int isAdmin;   /* 4 bytes */
    int priority;  /* 4 bytes */
};  /* total: 12 bytes */

struct WithBitField {
    unsigned int isActive : 1;
    unsigned int isAdmin  : 1;
    unsigned int priority : 3;
};  /* total: 4 bytes (fits in one int) */

---

Anonymous Bit Fields (Padding)

struct Register {
    unsigned int bit0   : 1;
    unsigned int        : 3;   /* skip 3 bits */
    unsigned int bit4   : 1;
};

---

Practical Use: Hardware Register Mapping

Bit fields are widely used in embedded systems to map hardware registers:

struct StatusRegister {
    unsigned int carry    : 1;
    unsigned int zero     : 1;
    unsigned int negative : 1;
    unsigned int overflow : 1;
    unsigned int          : 4;   /* reserved */
};

volatile struct StatusRegister *psr = (struct StatusRegister *)0x4000;
if (psr->zero) {
    /* result was zero */
}
Note: The exact layout of bit fields is implementation-defined and may differ between compilers. Avoid relying on bit field layout for portable code.