Decode Status Register into Human-Readable Flags

Code

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

void decode_status(uint8_t status_reg) {
    // Your logic here
    int a=1;
    if((status_reg&a)==a)
        printf("Power On\n");
    a=a<<1;
        if((status_reg&a)==a)
            printf("Error\n");
        a=a<<1;
        if((status_reg&a)==a)
            printf("Tx Ready\n");
         a=a<<1;
        if((status_reg&a)==a)
            printf("Rx Ready\n");
             a=a<<1;
        if((status_reg&a)==a)
            printf("Overheat\n");
         a=a<<1;
        if((status_reg&a)==a)
            printf("Undervoltage\n");
         a=a<<1;
        if((status_reg&a)==a)
            printf("Timeout\n");
         a=a<<1;
        if((status_reg&a)==a)
            printf("Reserved\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