Bird
Raised Fist0
Embedded Cprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using bit field structures in embedded C?
easy
A. To improve floating-point calculations
B. To create large arrays of integers
C. To store multiple small data values efficiently using specific bits
D. To allocate memory dynamically

Solution

  1. Step 1: Understand bit field structures

    Bit field structures allow storing data in a compact form by specifying the number of bits each field uses.
  2. Step 2: Identify the main purpose

    This helps save memory by packing multiple small values into fewer bytes, useful for flags or hardware registers.
  3. Final Answer:

    To store multiple small data values efficiently using specific bits -> Option C
  4. Quick Check:

    Bit fields save memory by using bits = A [OK]
Hint: Bit fields pack small data into bits to save memory [OK]
Common Mistakes:
  • Thinking bit fields create large arrays
  • Confusing bit fields with floating-point optimization
  • Assuming bit fields allocate memory dynamically
2. Which of the following is the correct syntax to declare a bit field of 3 bits named flags inside a struct?
easy
A. unsigned int flags(3);
B. unsigned int flags = 3;
C. unsigned int : flags 3;
D. unsigned int flags : 3;

Solution

  1. Step 1: Recall bit field syntax

    Bit fields use a colon after the variable name to specify the number of bits, e.g., flags : 3.
  2. Step 2: Check each option

    unsigned int flags : 3; correctly uses unsigned int flags : 3;. Others have invalid syntax.
  3. Final Answer:

    unsigned int flags : 3; -> Option D
  4. Quick Check:

    Bit field syntax uses colon = D [OK]
Hint: Bit field size follows colon after variable name [OK]
Common Mistakes:
  • Using equals sign instead of colon
  • Placing size before variable name
  • Using parentheses instead of colon
3. Given the struct definition:
struct S {
  unsigned int a : 2;
  unsigned int b : 3;
  unsigned int c : 3;
};

struct S s = {3, 5, 7};
printf("%u %u %u", s.a, s.b, s.c);

What is the output?
medium
A. 3 5 3
B. 3 5 7
C. 3 1 7
D. 3 5 0

Solution

  1. Step 1: Understand bit field sizes and value limits

    Field a has 2 bits (max 3), b has 3 bits (max 7), c has 3 bits (max 7).
  2. Step 2: Check assigned values and truncation

    Values assigned: a=3 (fits 2 bits), b=5 (fits 3 bits), c=7 (fits 3 bits). However, the value 7 assigned to c (3 bits) will be truncated if it exceeds the max value. Since 3 bits can hold max 7, 7 fits exactly, so no truncation. But the initializer list {3,5,7} assigns 7 to c, which is 3 bits wide. However, in practice, some compilers truncate values exceeding the bit field size modulo 2^n. Since 7 fits 3 bits, it should print 7. But the question's options suggest otherwise.
  3. Step 3: Re-examine the options

    3 5 3 is "3 5 3", which suggests truncation of 7 to 3, which is incorrect. 3 5 7 is "3 5 7", which matches the expected output.
  4. Final Answer:

    3 5 7 -> Option B
  5. Quick Check:

    Bit fields store values truncated to their bit size; 7 fits 3 bits = 3 5 7 [OK]
Hint: Check max value for bits; values truncate if too big [OK]
Common Mistakes:
  • Assuming 7 truncates to 3
  • Ignoring bit size limits
  • Confusing decimal and binary values
4. Identify the error in this bit field struct declaration:
struct Flags {
  unsigned int flag1 : 1;
  int flag2 : 2;
  unsigned flag3 : 3;
};
medium
A. No error; this is valid syntax
B. Missing bit size for flag3
C. Using signed int for bit field is not allowed
D. Mixing signed and unsigned bit fields causes error

Solution

  1. Step 1: Check bit field type rules

    Bit fields can be signed or unsigned integers. Using int or unsigned int is allowed.
  2. Step 2: Verify bit size for each field

    All fields specify bit sizes: flag1:1, flag2:2, flag3:3. The type unsigned is shorthand for unsigned int.
  3. Final Answer:

    No error; this is valid syntax -> Option A
  4. Quick Check:

    Signed and unsigned bit fields allowed = C [OK]
Hint: Signed or unsigned int bit fields both allowed [OK]
Common Mistakes:
  • Assuming signed bit fields are invalid
  • Thinking unsigned must be 'unsigned int'
  • Missing bit size causes error, but here all sizes present
5. You want to store the status of 10 different flags (each 1 bit) and a 6-bit error code in a struct using bit fields. Which struct layout uses the least memory on a 32-bit system?
hard
A. struct { unsigned int flags : 10; unsigned int error : 6; };
B. struct { unsigned int flag1 : 1; unsigned int flag2 : 1; unsigned int flag3 : 1; unsigned int flag4 : 1; unsigned int flag5 : 1; unsigned int flag6 : 1; unsigned int flag7 : 1; unsigned int flag8 : 1; unsigned int flag9 : 1; unsigned int flag10 : 1; unsigned int error : 6; };
C. struct { unsigned char flags : 10; unsigned char error : 6; };
D. struct { unsigned short flags : 10; unsigned short error : 6; };

Solution

  1. Step 1: Understand bit field packing

    Bit fields are packed into the base type size. Using one unsigned int for all bits packs them tightly.
  2. Step 2: Compare options

    struct { unsigned int flags : 10; unsigned int error : 6; }; packs all 16 bits (10 flags + 6 error) into one unsigned int (32 bits). struct { unsigned int flag1 : 1; unsigned int flag2 : 1; unsigned int flag3 : 1; unsigned int flag4 : 1; unsigned int flag5 : 1; unsigned int flag6 : 1; unsigned int flag7 : 1; unsigned int flag8 : 1; unsigned int flag9 : 1; unsigned int flag10 : 1; unsigned int error : 6; }; declares each flag separately, but still packed into unsigned int, so similar size but more verbose. struct { unsigned char flags : 10; unsigned char error : 6; }; uses unsigned char (8 bits), but 10 bits can't fit in 8 bits, so likely uses more space due to alignment. struct { unsigned short flags : 10; unsigned short error : 6; }; uses unsigned short (16 bits), which fits 16 bits exactly, but alignment may cause padding.
  3. Final Answer:

    struct { unsigned int flags : 10; unsigned int error : 6; }; -> Option A
  4. Quick Check:

    Pack all bits in one unsigned int for minimal size = B [OK]
Hint: Group bits in one unsigned int to minimize memory [OK]
Common Mistakes:
  • Using multiple small types causing padding
  • Assuming unsigned char can hold 10 bits
  • Declaring each flag separately wastes space
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard