Challenge - 5 Problems
Wake-up Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Wake-up source bitmask configuration output
What is the output of the following code snippet configuring wake-up sources using bitmasks?
Embedded C
/* Define wake-up sources as bitmasks */ #define WAKEUP_TIMER (1 << 0) #define WAKEUP_GPIO (1 << 1) #define WAKEUP_RTC (1 << 2) unsigned int wakeup_sources = 0; void enable_wakeup_sources() { wakeup_sources |= WAKEUP_TIMER; wakeup_sources |= WAKEUP_GPIO; } int main() { enable_wakeup_sources(); printf("0x%X", wakeup_sources); return 0; }
Attempts:
2 left
💡 Hint
Remember that bitwise OR combines bits set in both masks.
✗ Incorrect
WAKEUP_TIMER is bit 0 (value 1), WAKEUP_GPIO is bit 1 (value 2). ORing them sets bits 0 and 1, resulting in 0x3.
❓ Predict Output
intermediate2:00remaining
Wake-up source configuration with conditional flags
What is the output of this code that conditionally enables wake-up sources?
Embedded C
#include <stdio.h> #define WAKEUP_TIMER (1 << 0) #define WAKEUP_GPIO (1 << 1) #define WAKEUP_RTC (1 << 2) unsigned int configure_wakeup(int enable_timer, int enable_gpio, int enable_rtc) { unsigned int sources = 0; if (enable_timer) sources |= WAKEUP_TIMER; if (enable_gpio) sources |= WAKEUP_GPIO; if (enable_rtc) sources |= WAKEUP_RTC; return sources; } int main() { unsigned int result = configure_wakeup(1, 0, 1); printf("0x%X", result); return 0; }
Attempts:
2 left
💡 Hint
Check which bits correspond to enabled sources.
✗ Incorrect
enable_timer=1 sets bit 0 (1), enable_gpio=0 skips bit 1, enable_rtc=1 sets bit 2 (4). 1 + 4 = 5 (0x5).
🔧 Debug
advanced2:00remaining
Identify the error in wake-up source configuration
What error does this code produce when compiling?
Embedded C
#define WAKEUP_TIMER (1 << 0) #define WAKEUP_GPIO (1 << 1) unsigned int wakeup_sources = 0; void enable_sources() { wakeup_sources = wakeup_sources | WAKEUP_TIMER & WAKEUP_GPIO; } int main() { enable_sources(); return 0; }
Attempts:
2 left
💡 Hint
Check operator precedence between | and &.
✗ Incorrect
Bitwise AND (&) has higher precedence than OR (|), so expression evaluates as wakeup_sources | (WAKEUP_TIMER & WAKEUP_GPIO). Since WAKEUP_TIMER & WAKEUP_GPIO is 0, only wakeup_sources remains unchanged.
📝 Syntax
advanced2:00remaining
Find the syntax error in wake-up source configuration
Which option contains the correct syntax to enable both WAKEUP_TIMER and WAKEUP_GPIO bits?
Attempts:
2 left
💡 Hint
Check operator precedence and assignment operators.
✗ Incorrect
Option B correctly uses bitwise OR to set both bits. Option B uses AND which results in 0. Option B uses invalid operator '=+'. Option B has operator precedence issue, setting no bits.
🚀 Application
expert3:00remaining
Calculate number of enabled wake-up sources
Given the wakeup_sources variable holds enabled wake-up bits, what is the number of enabled sources after this code runs?
Embedded C
#define WAKEUP_TIMER (1 << 0) #define WAKEUP_GPIO (1 << 1) #define WAKEUP_RTC (1 << 2) #define WAKEUP_USB (1 << 3) unsigned int wakeup_sources = 0; void enable_sources() { wakeup_sources |= WAKEUP_TIMER; wakeup_sources |= WAKEUP_GPIO; wakeup_sources |= WAKEUP_RTC; wakeup_sources &= ~WAKEUP_GPIO; } int count_enabled_sources(unsigned int sources) { int count = 0; while (sources) { count += sources & 1; sources >>= 1; } return count; } int main() { enable_sources(); int enabled = count_enabled_sources(wakeup_sources); printf("%d", enabled); return 0; }
Attempts:
2 left
💡 Hint
Check which bits remain set after clearing WAKEUP_GPIO.
✗ Incorrect
Initially, TIMER, GPIO, RTC bits are set (bits 0,1,2). Then GPIO bit (bit 1) is cleared. So only TIMER and RTC remain set, total 2 bits.