C Bit Fields
In this page:
What are Bit Fields?
A bit field is a structure member declared with an explicit width in bits rather than a fixed built-in size, letting several small values share a single word of memory instead of each taking a whole int. This is most useful for things like flag registers or protocol headers where memory is tight and every bit is accounted for.
Example: What are Bit Fields?
#include <stdio.h>
struct Flags {
unsigned int status : 3;
};
int main() {
struct Flags f;
f.status = 5;
printf("%d", f.status);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Defining Bit Fields
You declare a bit field by writing a colon followed by the number of bits directly after the member name inside a struct, for example unsigned status : 3; to reserve exactly 3 bits for status. The compiler then packs that member alongside its neighbors instead of giving it a full byte or word.
Example: Defining Bit Fields
#include <stdio.h>
struct Flags {
unsigned status : 3;
};
int main() {
struct Flags f;
f.status = 6;
printf("%d", f.status);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Memory Packing Efficiency
A plain int member always costs 4 bytes regardless of the values it needs to hold, but a struct built from several small bit fields can pack many of those values into that same 4 bytes. This matters when you have thousands of records in memory, since the packing can meaningfully cut total memory usage.
Example: Memory Packing Efficiency
#include <stdio.h>
struct Packed {
unsigned a : 1;
unsigned b : 1;
unsigned c : 1;
};
int main() {
printf("%zu", sizeof(struct Packed));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Bit Field Value Limits
A bit field can only represent values that fit within the number of bits you gave it, so a 3-bit field can only hold 0 through 7 — assigning anything larger silently wraps around rather than raising an error. Always size your bit fields to the actual range of values you expect to store.
Example: Bit Field Value Limits
#include <stdio.h>
struct Flags {
unsigned value : 3;
};
int main() {
struct Flags f;
f.value = 9;
printf("%d", f.value);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Unsigned Bit Fields
Signed bit fields are risky because one of the bits is reserved for the sign, and how that sign bit gets extended when the value is read back is implementation-defined across compilers. Declaring bit fields as unsigned int avoids this ambiguity entirely and keeps the stored values behaving predictably.
Example: Unsigned Bit Fields
#include <stdio.h>
struct Flags {
unsigned int status : 3;
};
int main() {
struct Flags f;
f.status = 5;
printf("%u", f.status);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions