Decode Status Register into Human-Readable Flags

Code

#include <stdio.h>
#include <stdint.h>

#define FLAGS_NAMES{"Power On" ,"Error" , "Tx Ready", "Rx Ready", "Overheat", "Undervoltage", "Timeout", "Reserved"}
#define CHECK_BIT(reg, pos) ((reg) & (1 << pos))

void decode_status(uint8_t reg) {
    // Your logic here
    const char *flags[] = FLAGS_NAMES;

    for (uint8_t pos = 0; pos < 8; pos++){
        if(CHECK_BIT(reg, pos)){
            printf("%s\n", flags[pos]);
        }
    }
}

int main() {
    uint8_t reg;
    scanf("%hhu", &reg);
    decode_status(reg);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

13

Expected Output

Power On Tx Ready Rx Ready