11. Decode Status Register into Human-Readable Flags

Discussions2
Log in to post comments and replies.
You
Loading editor...
KangajanKuganathan

Code

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

#define POWER_ON            0
#define ERROR               1
#define TX_READY            2
#define RX_READY            3
#define OVER_HEAT           4
#define UNDER_VOLTAGE       5
#define TIME_OUT            6
#define RESERVED            7

void decode_status(uint8_t status_reg) {
    // Your logic here
    if (status_reg & (1 << POWER_ON)) {
        printf("Power On\n");
    } 
    if (status_reg & (1 << ERROR)) {
        printf("Error\n");
    }
    if (status_reg & (1 << TX_READY)) {
        printf("Tx Ready\n");
    }
    if (status_reg & (1 << RX_READY)) {
        printf("Rx Ready\n");
    }
    if (status_reg & (1 << OVER_HEAT)) {
        printf("Overheat\n");
    }
    if (status_reg & (1 << UNDER_VOLTAGE)) {
        printf("Undervoltage\n");
    }
    if (status_reg & (1 << TIME_OUT)) {
        printf("Timeout\n");
    }
    if (status_reg & (1 << RESERVED)) {
        printf("Reserved\n"); 
    }
}

int main() {
    uint8_t reg;
    scanf("%hhu", &reg);
    decode_status(reg);
    return 0;
}

Solving Approach

 

 

 

0
quoctoanahihi123
quoctoanahihi123
Jul 26 2026

#include <stdio.h>

#include <stdint.h>


 

void decode_status(uint8_t status_reg) {

    // Your logic here

    const char* status_flag_name[]={

        "Power On",

        "Error",

        "Tx Ready",

        "Rx Ready",

        "Overheat",

        "Undervoltage",

        "Timeout",

        "Reserved"

    };


 

    for (int i=0 ; i < 8 ; i++){

        if (status_reg & (1<<i)){

            printf("%s\n",status_flag_name[i]);

        }

    }

}


 

int main() {

    uint8_t reg;

    scanf("%hhu", &reg);

    decode_status(reg);

    return 0;

}

0