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;
✗ Incorrect
The number after the colon specifies the exact number of bits used, so 'flag' uses 4 bits.
Which of these is a benefit of bit field structures?
✗ Incorrect
Bit fields save memory by using only the needed bits. However, pointers to bit fields are not allowed, and layout can vary by compiler.
Can you take the address of a bit field member?
✗ Incorrect
You cannot take the address of a bit field member because they may not have a unique address in memory.
What does this declaration mean?<br>
struct S { unsigned int x : 2; unsigned int y : 3; };✗ Incorrect
The numbers after the colon specify the bits used: x uses 2 bits, y uses 3 bits.
Which is true about bit field ordering?
✗ Incorrect
Bit order and packing depend on compiler and hardware, so it may vary.
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.