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_READY        1U << 2
#define RX_READY        1U << 3
#define OVERHEAT        1U << 4
#define UNDERVOLTAGE    1U << 5
#define TIMEOUT         1U << 6
#define RESERVED        1U << 7

const uint8_t arrBits[] = {POWER_ON, ERROR, TX_READY, RX_READY, OVERHEAT, UNDERVOLTAGE, TIMEOUT, RESERVED};
const char* arrString[] = {"Power On", "Error", "Tx Ready", "Rx Ready",  "Overheat", "Undervoltage", "Timeout", "Reserved"};

void decode_status(uint8_t status_reg) {
    for(int i = 0; i < 8; i++){
        if(status_reg & arrBits[i]){
            printf("%s\n", arrString[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