All submissions

Decode Status Register into Human-Readable Flags

Code

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

#define POWER_ON 0x1
#define EROOR 0x2
#define TX_READY 0x4
#define RX_READY 0x8
#define OVERHEAT 0x10
#define UNDER_VOLTAGE 0x20
#define TIMEOUT 0x40
#define RESERVED 0x80
const char *str[] = {"Power On", "Error", "Tx Ready", "Rx Ready", "Overheat", "Undervoltage", "Timeout", "Reserved"};
uint8_t mask[] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};

void decode_status(uint8_t status_reg) {
    for(int i = 0; i < 8; i++)
        if(status_reg & mask[i])
            printf("%s\n", str[i]);

 /*           
    if(status_reg & POWER_ON)
        printf("Power On\n");
    if(status_reg & EROOR)
        printf("Error\n");
    if(status_reg & TX_READY)
        printf("Tx Ready\n");
    if(status_reg & RX_READY)
        printf("Rx Ready\n");
    if(status_reg & OVERHEAT)
        printf("Overheat\n");
    if(status_reg & UNDER_VOLTAGE)
        printf("Undervoltage\n");
    if(status_reg & TIMEOUT)
        printf("Timeout\n");
    if(status_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