All submissions

Decode Status Register into Human-Readable Flags

Code

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

struct UART_StatusRegister {
    uint8_t poweron : 1;
    uint8_t error : 1;
    uint8_t tx_ready : 1;
    uint8_t rx_ready : 1;
    uint8_t overheat : 1;
    uint8_t undervoltage : 1;
    uint8_t timeout : 1;
    uint8_t reserved : 1;
};

union UART_ControlRegister1 {
    struct UART_StatusRegister reg;
    uint8_t reg1;
};

void decode_status(uint8_t status_reg) {
    // Your logic here
    union UART_ControlRegister1 reg2;
    reg2.reg1 = status_reg;
    if (reg2.reg.poweron) {
        printf("Power On\n");
    }
    if (reg2.reg.error) {
        printf("Error\n");
    }

    if (reg2.reg.tx_ready) {
        printf("Tx Ready\n");
    }

    if (reg2.reg.rx_ready) {
        printf("Rx Ready\n");
    }

    if (reg2.reg.overheat) {
        printf("Overheat\n");
    }
    if (reg2.reg.undervoltage) {
        printf("Undervoltage\n");
    }

    if (reg2.reg.timeout) {
        printf("Timeout\n");
    }

    if (reg2.reg.reserved) {
        printf("Reserved\n");
    }
}

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

Solving Approach

 

 

 

Loading...

Input

13

Expected Output

Power On Tx Ready Rx Ready