Decode Status Register into Human-Readable Flags

Code

#include <stdio.h>
#include <stdint.h>
// #define Power_On 0
// #define Error    1
// #define Tx_Ready 2
// #define Rx_Ready 3
// #define Overheat 4
// #define Undervoltage 5
// #define Timeout 6
// #define Reserved 7
const char* Bit_Meaning[8] = {
    "Power On",
    "Error",
    "Tx Ready",
    "Rx Ready",
    "Overheat",
    "Undervoltage",
    "Timeout",
    "Reserved"
};

void decode_status(uint8_t status_reg) {
    // Your logic here
//     if(status_reg & (1U <<Power_On)) printf("Power On\n");
//     if(status_reg & (1U <<Error)) printf("Error\n");
//     if(status_reg & (1U <<Tx_Ready)) printf("Tx Ready\n");
//     if(status_reg & (1U <<Rx_Ready)) printf("Rx Ready\n");
//     if(status_reg & (1U <<Overheat)) printf("Overheat\n");
//     if(status_reg & (1U <<Undervoltage)) printf("Undervoltage\n");
//     if(status_reg & (1U <<Timeout)) printf("Timeout\n");
//     if(status_reg & (1U <<Timeout)) printf("Reserved\n");
// 
    for(int i = 0; i < 8; i++){
        if((status_reg >> i) & 1){
            printf("%s\n", Bit_Meaning[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