Decode Status Register into Human-Readable Flags

Code

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

void decode_status(uint8_t status_reg) {
    if((1<<0)&status_reg)
     printf("Power On\n");

    if((1<<1)&status_reg)
     printf("Error\n");
    if((1<<2)&status_reg)
     printf("Tx Ready\n");
    if((1<<3)&status_reg)
     printf("Rx Ready\n");
    if((1<<4)&status_reg)
     printf("Overheat\n");
    if((1<<5)&status_reg)
     printf("Undervoltage\n");
    if((1<<6)&status_reg)
     printf("Timeout\n");
    if((1<<7)&status_reg)
     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