0
0
Embedded Cprogramming~5 mins

Bit field structures in Embedded C - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a bit field structure in C?
A bit field structure in C is a special way to store multiple small integer values using a specific number of bits within a single integer variable. It helps save memory by packing data tightly.
Click to reveal answer
beginner
How do you declare a bit field inside a struct?
You declare a bit field by specifying a colon and the number of bits after the variable name inside a struct. For example:
struct Example { unsigned int flag : 1; };
means 'flag' uses 1 bit.
Click to reveal answer
intermediate
Why use bit fields instead of normal variables?
Bit fields save memory by using only the needed bits instead of full bytes or words. This is useful in embedded systems where memory is limited and you want to store many small flags or values efficiently.
Click to reveal answer
intermediate
What is a limitation of bit field structures?
Bit fields may have portability issues because the order and packing of bits can depend on the compiler and hardware. Also, you cannot take the address of a bit field member.
Click to reveal answer
beginner
Example: What does this struct mean?<br>
struct Flags { unsigned int a : 3; unsigned int b : 5; unsigned int c : 1; };
This struct has three bit fields:<br>- 'a' uses 3 bits (can hold values 0 to 7)<br>- 'b' uses 5 bits (0 to 31)<br>- 'c' uses 1 bit (0 or 1)<br>All together they pack into a small space instead of using full integers each.
Click to reveal answer
How many bits does this bit field use?<br>
unsigned int flag : 4;
A1 byte
B4 bits
C4 bytes
DDepends on the compiler
Which of these is a benefit of bit field structures?
AThey guarantee the same layout on all compilers
BThey allow pointers to bit fields
CThey increase program speed always
DThey save memory by packing bits tightly
Can you take the address of a bit field member?
AYes, always
BOnly if it is unsigned
CNo, never
DOnly if it is 1 bit
What does this declaration mean?<br>
struct S { unsigned int x : 2; unsigned int y : 3; };
Ax uses 2 bits, y uses 3 bits
Bx and y each use 1 byte
Cx and y are normal integers
Dx uses 3 bits, y uses 2 bits
Which is true about bit field ordering?
ABit order depends on compiler and hardware
BBit order is random
CBit order is fixed by the C standard
DBit order is always left to right
Explain what a bit field structure is and why it is useful in embedded C programming.
Think about how you can store many small flags efficiently.
You got /4 concepts.
    Describe how to declare bit fields inside a struct and mention one limitation of using them.
    Remember the colon after variable name and bit count.
    You got /3 concepts.