0
0
Embedded Cprogramming~20 mins

Bit field structures in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bit Field Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of bit field structure size and values
What is the output of this C code when compiled and run on a typical 32-bit system?
Embedded C
typedef struct {
    unsigned int a:3;
    unsigned int b:5;
    unsigned int c:6;
} Bits;

#include <stdio.h>
int main() {
    Bits x = {5, 17, 33};
    printf("Size: %zu\n", sizeof(x));
    printf("a=%u b=%u c=%u\n", x.a, x.b, x.c);
    return 0;
}
A
Size: 4
a=5 b=17 c=1
B
Size: 4
a=5 b=17 c=33
C
Size: 2
a=5 b=17 c=1
D
Size: 2
a=5 b=17 c=33
Attempts:
2 left
💡 Hint
Remember that bit fields pack bits tightly and the size depends on total bits used.
🧠 Conceptual
intermediate
1:30remaining
Bit field overflow behavior
Given this bit field declaration:
unsigned int x:3;
What happens if you assign x = 10; ?
ACompilation error due to overflow
Bx stores the value 2 because only the lowest 3 bits are kept
Cx stores the value 10 without any change
Dx stores 0 because 10 is out of range
Attempts:
2 left
💡 Hint
Think about how many bits 3 bits can hold and what happens to extra bits.
🔧 Debug
advanced
2:00remaining
Why does this bit field struct cause unexpected output?
Consider this code snippet:
typedef struct {
    unsigned int flag:1;
    unsigned int value:7;
} Data;

int main() {
    Data d = {1, 130};
    printf("flag=%u value=%u\n", d.flag, d.value);
    return 0;
}

What is the reason for the unexpected output of value?
AThe struct is not properly aligned causing garbage values
BThe flag bit overlaps with value causing corruption
CThe value 130 exceeds 7 bits and wraps around, causing unexpected value
DThe printf format specifier is incorrect for bit fields
Attempts:
2 left
💡 Hint
Check the maximum value storable in 7 bits and what happens if you assign a bigger number.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in bit field declaration
Which option contains a syntax error in bit field declaration?
Astruct S { unsigned int a:4; unsigned int b;:5 };
Bstruct S { unsigned int a:4; unsigned int b:5; };
Cstruct S { unsigned int a:4; unsigned b:5; };
Dstruct S { unsigned int a:4; int b:5; };
Attempts:
2 left
💡 Hint
Look carefully at the colon placement in bit field syntax.
🚀 Application
expert
2:30remaining
Calculate total bits and size of nested bit field structs
Given these nested structs:
typedef struct {
    unsigned int x:4;
    unsigned int y:4;
} Inner;

typedef struct {
    Inner a;
    unsigned int z:8;
} Outer;

#include 
int main() {
    printf("Size of Outer: %zu\n", sizeof(Outer));
    return 0;
}

What is the output of the program?
ASize of Outer: 4
BSize of Outer: 3
CSize of Outer: 1
DSize of Outer: 2
Attempts:
2 left
💡 Hint
Calculate total bits in Inner and Outer and consider alignment and padding.