Consider the following FreeRTOS code snippet using event groups. What will be the value of uxBits after the operations?
EventGroupHandle_t xEventGroup = xEventGroupCreate(); EventBits_t uxBits; xEventGroupSetBits(xEventGroup, (1 << 0) | (1 << 2)); uxBits = xEventGroupClearBits(xEventGroup, (1 << 0)); // uxBits now holds the bits that were set before clearing printf("0x%X", (unsigned int)uxBits);
Remember that xEventGroupClearBits returns the bits set before clearing.
The bits set initially are 0x1 (bit 0) and 0x4 (bit 2), combined as 0x5. Clearing bit 0 returns the original bits before clearing, which is 0x5.
Given the following FreeRTOS code, what value is printed (the return value of xEventGroupClearBits)?
EventGroupHandle_t xEventGroup = xEventGroupCreate(); xEventGroupSetBits(xEventGroup, (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); EventBits_t uxRemaining = xEventGroupClearBits(xEventGroup, (1 << 1) | (1 << 3)); printf("0x%X", (unsigned int)uxRemaining);
Clear bits 1 and 3 but note what xEventGroupClearBits returns.
The bits set initially are 0xF (bits 0,1,2,3). Clearing bits 1 and 3 returns the bits set before clearing, which is 0xF.
Examine the code below. Why does the event bit clearing not work as expected?
EventGroupHandle_t xEventGroup = xEventGroupCreate(); xEventGroupSetBits(xEventGroup, (1 << 2)); // Attempt to clear bit 2 xEventGroupClearBits(xEventGroup, 2); EventBits_t uxBits = xEventGroupGetBits(xEventGroup); printf("0x%X", (unsigned int)uxBits);
Check how bits are represented and cleared.
The code uses 2 instead of (1 << 2). 2 is bit 1, not bit 2. So it clears the wrong bit.
Which of the following code snippets will cause a syntax error when trying to set event bits?
Look for missing commas or parentheses.
Option A is missing a comma between the function arguments, causing a syntax error.
Given the following sequence of FreeRTOS event group operations, how many bits remain set at the end?
EventGroupHandle_t xEventGroup = xEventGroupCreate(); xEventGroupSetBits(xEventGroup, (1 << 0) | (1 << 1) | (1 << 2)); xEventGroupClearBits(xEventGroup, (1 << 1)); xEventGroupSetBits(xEventGroup, (1 << 3)); xEventGroupClearBits(xEventGroup, (1 << 0) | (1 << 2)); EventBits_t uxBits = xEventGroupGetBits(xEventGroup); printf("%u", (unsigned int)__builtin_popcount(uxBits));
Track each set and clear operation carefully.
Initially bits 0,1,2 set. Clear bit 1 leaves bits 0 and 2. Set bit 3 adds bit 3. Clear bits 0 and 2 leaves only bit 3 set. So only 1 bit remains set.
