#include <stdio.h> #include <stdint.h> static char *flags[] = {"Power On", "Error", "Tx Ready", "Rx Ready", "Overheat", "Undervoltage", "Timeout", "Reserved"}; void decode_status(uint8_t status_reg) { // Your logic here volatile uint8_t i; for(i=0;i<8;i++){ if(status_reg&(1<<i)){ printf("%s\n", flags[i]); } } } int main() { uint8_t reg; scanf("%hhu", ®); decode_status(reg); return 0; }
Solving Approach
Set up an static array of pointers which are encoded for what each bit represents. In the function, check all the bits from lsb and if that bit is set, then print the corresponding flag name.
Test Cases
Test Results
Input
13
Expected Output
Power On Tx Ready Rx Ready