Decode Status Register into Human-Readable Flags

Code

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

#define  POWER_ON   1U << 0
#define  ERROR      1U << 1
#define  TX_RDY     1U << 2
#define  RX_RDY     1U << 3
#define  OVERHEAT   1U << 4
#define  UNDERVOLT  1U << 5
#define  TIMEOUT    1U << 6
#define  RESERVE    1U << 7

const uint8_t arr[]  = {POWER_ON, ERROR, TX_RDY, RX_RDY, OVERHEAT, UNDERVOLT, TIMEOUT,RESERVE };
const char *status[] = {"Power On","Error","Tx Ready", "Rx Ready","Overheat","Undervoltage","Timeout","Reserved"};

void decode_status(uint8_t status_reg) {
    // Your logic here
    for(uint8_t i = 0; i < 8; i++)
    {
        if (arr[i] & status_reg)
        {
            printf("%s\n", status[i]);
        }

    }
}

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