Decode Status Register into Human-Readable Flags

Code

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

void decode_status(uint8_t reg) {
    // Your logic here
    if(reg&(0x01)){
        printf("Power On");
        printf("\n");
    }
    if(reg&(0x02)){
        printf("Error");
        printf("\n");
    }
    if(reg&(0x04)){
        printf("Tx Ready");
        printf("\n");
    }
    if(reg&(0x08)){
        printf("Rx Ready");
        printf("\n");
    }
    if(reg&(0x10)){
        printf("Overheat");
        printf("\n");
    }
    if(reg&(0x20)){
        printf("Undervoltage");
        printf("\n");
    }
    if(reg&(0x40)){
        printf("Timeout");
        printf("\n");
    }
    if(reg&(0x80)){
        printf("Reserved");
    }
}

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