5.1.23 · D1C Programming

Foundations — Bit fields in structs

1,524 words7 min readBack to topic

Before you can read the parent note, you must own every word and symbol it throws at you. This page builds each one from absolute zero, in an order where every idea rests on the one before it. Parent topic: Bit fields in structs.


1. The bit — the atom of everything

Picture a light switch on a wall. It is either OFF or ON — never "half". That is exactly one bit. Everything in the parent note is ultimately made of these switches.


2. Binary — writing numbers with only 0 and 1

You count with ten symbols (0–9). A computer counts with only two. That is binary (base 2).

Why the topic needs this: the parent constantly says things like "". You cannot follow any overflow or truncation line without reading binary.


3. Powers of two and the symbol

The parent's key formula is . Let's earn it.

Recall Check the doubling by hand

1 bit ::: 2 patterns (0, 1) 2 bits ::: 4 patterns (00, 01, 10, 11) 3 bits ::: 8 patterns (000 … 111), biggest value


4. Bytes and the storage unit

The tool Data types and sizeof is what tells you a storage unit is 32 bits wide; Memory alignment and padding explains why the compiler rounds up to whole units (giving sizeof == 4, not 1).


5. The struct — a labelled box of members

Bit fields are just struct members with an extra " : width" tag telling the compiler "give me only this many bits." So you must be comfortable with the plain struct first — the colon-width syntax is the only new thing on top.

struct Flags {          // the struct: a labelled box
    unsigned int level : 3;   // member "level", but only 3 bits wide
};

6. Signed vs unsigned — where a sign hides


7. The symbols &, %, and : you'll meet

The manual, no-bit-field way to poke individual bits uses Bitwise operators (& | ^ << >>); bit fields are the compiler doing that bookkeeping for you. The classic reason to want any of this is Embedded systems / hardware registers.


Prerequisite map

Bit two-state switch

Binary base two

Powers of two 2 to the N

Byte 8 bits

Storage unit int box

Struct labelled members

Signed vs unsigned

Symbols and address mod

Bit fields in structs


Equipment checklist

State each aloud before reading the parent note:

What are the only two states of a bit?
0 (off) and 1 (on).
What is in base ten?
5 (one 4 + one 1).
What does count for an N-bit field?
The number of distinct values it can hold; biggest value is .
How many bits are in a byte?
8.
What is a "storage unit" for bit fields on a typical system?
An int, i.e. 32 bits, that the compiler packs fields into.
What is a struct member, and how do you reach it?
A named variable inside a struct, reached with a dot: s.member.
Why does a signed 3-bit field only reach 0..3 on the positive side?
Its top bit is spent on the sign, halving the positive range (−4..3).
What does &x ask, and why is it illegal for a bit field?
"Where does x live in memory?" — bit fields aren't byte-addressable, so they have no address.
What does equal, and where does it appear?
5 — it is the truncation of assigning 13 to a 3-bit field.