Bit Fields
Pack several small values into the bits of one integer inside a struct.
Intermediate
struct Status {
unsigned ready : 1;
unsigned error : 1;
unsigned mode : 3; // 0–7
};
Bit fields save memory, but their exact layout depends on the compiler, so for hardware registers and network packets many developers prefer explicit masks and shifts, which are portable.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.