Decode Status Register into Human-Readable Flags

Code

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

enum Bit_to_Flag
    {   Power_On,
        Error,  //Earlier was thinking of using Enum but lite. Also when kept this enum faced an error. Apparently we cannot keep spaces in Enum elements.Bruh
        Tx_Ready,
        Rx_Ready,
        Overheat,
        Undervoltage,
        Timeout,
        Reserved
    };

//const char *Mapping [] = {{"Power On"}, {"Error"},{"Tx Ready"} ,{"Rx Ready"} , {"Overheat"} , {"Undervoltage"} ,{"Timeout"}, {"Reserved"}};
//Alternative to this is using a 2D array:
const char Mapping [][13] = {"Power On","Error","Tx Ready",  "Rx Ready", "Overheat",  "Undervoltage","Timeout","Reserved"};  //Value of 13 taken since 13 is the max size of the character +1 among all the elemets Biggest string is Undervoltage with 12 chars.
void decode_status(uint8_t status_reg) {
    // Your logic here
    uint8_t n =0;

    for(n;n<9;n++)
    {
        if(status_reg & (1<<n))
        {
            printf("%s\n",Mapping[n]);
        }
        else
        {
            continue;
        }
    }
}

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