Decode Status Register into Human-Readable Flags

Code

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

typedef struct{
    uint8_t pwr_on:1;
    uint8_t err:1;
    uint8_t tx_rdy:1;
    uint8_t rx_rdy:1;
    uint8_t overheat:1;
    uint8_t under_volt:1;
    uint8_t timeout:1;
    uint8_t rev:1;
} uart_flags_t;

typedef union{
    uart_flags_t uart_flags;
    uint8_t reg;
} uart_flags_u;

void decode_status(uint8_t status_reg) {
    // Your logic here
    uart_flags_u uart_flags;
    uart_flags.reg = status_reg;

    if(uart_flags.uart_flags.pwr_on){
        printf("Power On\n");
    }
    if(uart_flags.uart_flags.err){
        printf("Error\n");
    }
    if(uart_flags.uart_flags.tx_rdy){
        printf("Tx Ready\n");
    }
    if(uart_flags.uart_flags.rx_rdy){
        printf("Rx Ready\n");
    }
    if(uart_flags.uart_flags.overheat){
        printf("Overheat\n");
    }
    if(uart_flags.uart_flags.under_volt){
        printf("Undervoltage\n");
    }
    if(uart_flags.uart_flags.timeout){
        printf("Timeout\n");
    }
    if(uart_flags.uart_flags.timeout){
        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