Setting and clearing event bits in FreeRTOS - Time & Space Complexity
When working with FreeRTOS event bits, it's important to understand how the time to set or clear bits changes as the number of bits grows.
We want to know how the cost of these operations grows when more event bits are involved.
Analyze the time complexity of the following code snippet.
// Set and clear event bits example
EventGroupHandle_t eventGroup = xEventGroupCreate();
// Set bits 0 and 1
xEventGroupSetBits(eventGroup, (1 << 0) | (1 << 1));
// Clear bit 0
xEventGroupClearBits(eventGroup, (1 << 0));
This code creates an event group, sets two bits, and then clears one bit in the group.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking and updating bits inside the event group's bit field.
- How many times: The operation runs once per call, processing all bits in a fixed-size integer (usually 32 bits).
The time to set or clear bits depends on the number of bits in the event group, which is fixed (commonly 32 bits).
| Input Size (number of bits) | Approx. Operations |
|---|---|
| 10 | 10 checks and updates |
| 32 | 32 checks and updates |
| 64 | 64 checks and updates (if supported) |
Pattern observation: The operations grow linearly with the number of bits, but since the bit count is fixed and small, the time stays constant in practice.
Time Complexity: O(1)
This means setting or clearing event bits takes about the same time regardless of how many bits are set, because the bit field size is fixed.
[X] Wrong: "Setting multiple bits takes longer for each bit set, so time grows with the number of bits set."
[OK] Correct: The operation checks all bits in a fixed-size field each time, so time depends on the total bit field size, not how many bits are set.
Understanding how fixed-size bit operations work helps you reason about efficient signaling and synchronization in embedded systems.
"What if the event group supported a variable number of bits instead of a fixed size? How would the time complexity change?"
Practice
xEventGroupSetBits() in FreeRTOS?Solution
Step 1: Understand the function name
xEventGroupSetBits()suggests setting bits, which means signaling events.Step 2: Compare with other functions
xEventGroupClearBits()is used to clear bits, so setting bits is done byxEventGroupSetBits().Final Answer:
To set (signal) specific bits in an event group -> Option CQuick Check:
Set bits = signal events [OK]
- Confusing setting bits with clearing bits
- Thinking it creates or deletes event groups
- Mixing up function names
myEventGroup?Solution
Step 1: Identify the correct function for clearing bits
The function to clear bits isxEventGroupClearBits().Step 2: Check the syntax
The correct syntax isxEventGroupClearBits(eventGroupHandle, bitsToClear);. xEventGroupClearBits(myEventGroup, 0x03); matches this exactly.Final Answer:
xEventGroupClearBits(myEventGroup, 0x03); -> Option DQuick Check:
Clear bits syntax = xEventGroupClearBits(handle, bits) [OK]
- Using xEventGroupSetBits to clear bits
- Using incorrect function names
- Missing parameters or wrong order
EventBits_t bits; bits = xEventGroupSetBits(myEventGroup, 0x05); bits = xEventGroupClearBits(myEventGroup, 0x01);
What is the value of
bits after these calls if the initial bits were 0x00?Solution
Step 1: First call - xEventGroupSetBits(myEventGroup, 0x05)
Initial event group: 0x00. Returns bits BEFORE setting: 0x00 (event group becomes 0x05).bits = 0x00.Step 2: Second call - xEventGroupClearBits(myEventGroup, 0x01)
Event group now: 0x05. Returns bits BEFORE clearing: 0x05 (event group becomes 0x04).bits = 0x05.Final Answer:
0x00 -> Option AQuick Check:
Return value = pre-operation bits [OK]
- Confusing return value (pre-op) with post-op event group state
- Miscalculating bitwise operations on event group
- Forgetting return value overwrites 'bits' each call
EventGroupHandle_t myEventGroup; myEventGroup = xEventGroupCreate(); xEventGroupClearBits(0x01, myEventGroup);
Solution
Step 1: Check function parameter order
xEventGroupClearBits()expects (EventGroupHandle_t, EventBits_t). The code passes (0x01, myEventGroup), which is reversed.Step 2: Confirm return type of xEventGroupCreate()
xEventGroupCreate()returns an EventGroupHandle_t, so assignment is correct.Final Answer:
Arguments to xEventGroupClearBits are reversed -> Option AQuick Check:
Function parameters order matters [OK]
- Swapping parameters in API calls
- Assuming wrong return types
- Ignoring compiler warnings
Solution
Step 1: Set bits 0x01 and 0x04 together
Setting bits 0x05 (binary 0101) signals both events at once.Step 2: Clear only bit 0x01
Clearing bit 0x01 removes that event signal but leaves 0x04 set.Final Answer:
xEventGroupSetBits(myEventGroup, 0x05); xEventGroupClearBits(myEventGroup, 0x01); -> Option BQuick Check:
Set both bits then clear one bit [OK]
- Clearing bits before setting them
- Clearing wrong bits
- Not combining bits correctly
Ready to prove it?
Pass the test and this pattern moves unknown
