C Reference ยท Types and Literals
Bit-fields in C
Reference for C bit-fields: declaring members with a width, allowed types, signedness pitfalls, layout being implementation-defined, and when to use masks instead.
A bit-field is a struct member that occupies a stated number of bits. You write a colon and a width after the member name: unsigned int mode : 3; creates a field that can hold the values 0 to 7. Neighbouring bit-fields are usually packed together into the same storage unit, which saves memory for flags and small counters.
Bit-fields are convenient but their layout is up to the compiler. Do not use them for file formats, network protocols or hardware registers that must match an exact bit position unless your ABI documents the layout.
struct flags { unsigned int read : 1; unsigned int write : 1; unsigned int mode : 3; };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
struct name { type member : width; /* named bit-field */ type : width; /* unnamed: padding */ type : 0; /* zero width: align to next unit */ };
| Part | Description |
|---|---|
type |
_Bool/bool, signed int, unsigned int, or (C23) a _BitInt(N) type; compilers often accept other integer types. |
width |
Constant expression from 0 up to the width of type. |
: 0 |
Unnamed zero-width field that forces the next field into a new allocation unit. |
Returns: reading a field gives its value promoted to int or unsigned int.
Errors / undefined behavior: taking the address of a bit-field (&s.x) is a constraint violation. Storing a value too large for a signed field is implementation-defined; for an unsigned field it wraps modulo 2^width. Using sizeof or offsetof on a bit-field is not allowed.
Usage notes
- Layout is implementation-defined: bit order within a unit (least significant first or most significant first), padding, alignment, and whether a field may straddle two units all vary between compilers and ABIs.
- Plain
intbit-fields may be signed or unsigned (it is implementation-defined). Always writesigned intorunsigned int. - A one-bit signed field can hold only 0 and -1. Use unsigned for flags.
- Not thread-safe by default. Adjacent bit-fields share storage; writing one can read-modify-write its neighbours, so two threads updating different fields of one word can race.
- For exact binary formats use masks and shifts, and fixed-width types from
<stdint.h>. - Initialization and assignment work normally. Printing needs
%uor%dafter promotion; there is no special format. - Space savings are modest. The compiler adds instructions for masking, so arrays of millions of small values may be slower than plain
unsigned char. boolfields (bool ok : 1;) are well defined and read as 0 or 1.