Decode Status Register into Human-Readable Flags

Code

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

enum Flags{
    POWER_ON = (1U<<0),
    ERROR = (1U<<1),
    TX_READY = (1U<<2),
    RX_READY = (1U<<3),
    OVERHEAT = (1U<<4),
    UNDERVOLTAGE = (1U<<5),
    TIMEOUT = (1U<<6),
    RESERVED = (1<<7)
};

void decode_status(uint8_t reg) {
    if (reg & POWER_ON)     printf("Power On\n");
    if (reg & ERROR)        printf("Error\n");
    if (reg & TX_READY)     printf("Tx Ready\n");
    if (reg & RX_READY)     printf("Rx Ready\n");
    if (reg & OVERHEAT)     printf("Overheat\n");
    if (reg & UNDERVOLTAGE) printf("Undervoltage\n");
    if (reg & TIMEOUT)      printf("Timeout\n");
    if (reg & RESERVED)     printf("Reserved\n");
}

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