11. Decode Status Register into Human-Readable Flags

Discussions4
Log in to post comments and replies.
PatelTushang
PatelTushang
Sep 05 2026

#include <stdio.h>

#include <stdint.h>


 

char *data_decode(uint8_t val)

{

    switch(val)

    {

        case 0:

            return "Power On";

        break;

        case 1:

            return "Error";

        break;

        case 2:

            return "Tx Ready";

        break;

        case 3:

            return "Rx Ready";

        break;

        case 4:

            return "Overheat";

        break;

        case 5:

            return "Undervoltage";

        break;

        case 6:

            return "Timeout";

        break;

        case 7:

            return "Reserved";

        break;

    }

}


 

void decode_status(uint8_t status_reg) {

    // Your logic here

    for(int i = 0; i <= 7; i++)

    {

        uint8_t mask = 1U << i;

        if(status_reg & mask)

        {

            printf("%s\n",data_decode(i));

        }

    }

}


 

int main() {

    uint8_t reg;

    scanf("%hhu", &reg);

    decode_status(reg);

    return 0;

}

0
SaiSuddhirABae22b013
#include <stdio.h>
#include <stdint.h>
#define CHECK_BIT(reg,pos) ((reg & (1<<pos))>>pos)
void decode_status(uint8_t reg) {
    if (CHECK_BIT(reg,0))  printf("Power On\n");
    if (CHECK_BIT(reg,1))  printf("Error\n");
    if (CHECK_BIT(reg,2))  printf("Tx Ready\n"); 
    if (CHECK_BIT(reg,3))  printf("Rx Ready\n");
    if (CHECK_BIT(reg,4))  printf("Overheat\n");
    if (CHECK_BIT(reg,5))  printf("Undervoltage\n");
    if (CHECK_BIT(reg,6))  printf("Timeout\n");
    if (CHECK_BIT(reg,7))  printf("Reserved");

}
int main() {
    uint8_t reg;
    scanf("%hhu", &reg);
    decode_status(reg);
    return 0;
}
0
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