Decode Status Register into Human-Readable Flags

Code

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

void decode_status(uint8_t status_reg) {
    // Your logic here
    if(status_reg &(1U<<0)) //Setting Bit 0
    {
        printf("Power On");
        printf("\n");
    }
    if(status_reg & (1U<<1)) //Setting Bit 1
    {
        printf("Error");
        printf("\n");
    }
    if(status_reg &(1U<<2)) //Setting Bit 2
    {
        printf("Tx Ready");
        printf("\n");
    }
    if(status_reg &(1U<<3)) //Setting Bit 3
    {
        printf("Rx Ready");
        printf("\n");
    }
    if(status_reg &(1U<<4)) //Setting Bit 4
    {
        printf("Overheat");
        printf("\n");
    }
    if(status_reg &(1U<<5)) //Setting Bit 5
    {
        printf("Undervoltage");
        printf("\n");
    }
    if(status_reg &(1U<<6)) //Setting Bit 6
    {
        printf("Timeout");
        printf("\n");
    }
    if(status_reg &(1U<<7)) //Setting Bit 7
    {
        printf("Reserved");
        printf("\n");
    }

}

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