0
0
Embedded Cprogramming~10 mins

Bit field structures in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Bit field structures
Define struct with bit fields
Declare variable of struct type
Assign values to bit fields
Store values in memory (packed bits)
Access bit fields individually
Use bit fields in logic or output
Bit field structures let you store small pieces of data in specific bits inside a struct, saving memory and allowing easy access.
Execution Sample
Embedded C
struct Flags {
  unsigned int a:1;
  unsigned int b:3;
  unsigned int c:4;
};

struct Flags f = {1, 5, 10};
printf("a=%u b=%u c=%u", f.a, f.b, f.c);
Defines a struct with 3 bit fields, assigns values, and prints each field.
Execution Table
StepActionBit FieldValue AssignedMemory BitsOutput
1Define struct with bit fields----
2Declare variable f--00000000 (initial)-
3Assign f.a = 1a (1 bit)100000001-
4Assign f.b = 5b (3 bits)5 (binary 101)0010101-
5Assign f.c = 10c (4 bits)10 (binary 1010)10101011-
6Print values---a=1 b=5 c=10
7End---Program ends
💡 All bit fields assigned and printed; program ends.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
f.a01111
f.b00555
f.c0001010
Memory bits000000000000000100101011010101110101011
Key Moments - 3 Insights
Why does assigning 5 to f.b (3 bits) work even though 5 is larger than 3?
The 3 bits can hold values from 0 to 7 (2^3-1). Since 5 is within this range, it fits correctly as shown in step 4 of the execution_table.
What happens if we assign a value larger than the bit field can hold?
Values larger than the bit field size get truncated to fit the bits. For example, if f.b had 3 bits and we assigned 10, only the lowest 3 bits (010) would be stored, causing unexpected values.
How are multiple bit fields packed in memory?
Bit fields are packed next to each other in the order declared, filling bits from least significant to more significant as shown in the Memory bits column in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the binary value stored in memory after assigning f.b = 5?
A00000101
B00001011
C00000001
D10101001
💡 Hint
Check the Memory Bits column at step 4 in the execution_table.
At which step does the bit field f.c get its value assigned?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look at the Action and Bit Field columns in the execution_table.
If we assign f.a = 2, what will happen to the stored value?
AIt stores 2 correctly in 1 bit
BIt stores 1 because 2 is truncated to 1 bit
CIt stores 0 because 2 is truncated to 1 bit
DIt causes a compile error
💡 Hint
Recall that 1 bit can only store 0 or 1; values are truncated to fit as explained in key_moments.
Concept Snapshot
Bit field structures let you store small integers in specific bits inside a struct.
Syntax: unsigned int name : number_of_bits;
Values assigned fit in the bits or get truncated.
Bit fields are packed in memory in declaration order.
Access each bit field like a normal struct member.
Useful for saving memory in embedded systems.
Full Transcript
This visual execution trace shows how bit field structures work in embedded C. First, a struct with three bit fields is defined: a with 1 bit, b with 3 bits, and c with 4 bits. Then a variable f of this struct type is declared and initialized with values 1, 5, and 10 respectively. Each assignment updates the memory bits accordingly, packing the bits next to each other. The output prints the values stored in each bit field. Key moments explain how values fit or truncate in bit fields and how bits are packed. The quizzes test understanding of memory representation and bit truncation. This helps beginners see how bit fields store data efficiently and how to use them safely.