Decode Status Register into Human-Readable Flags

Code

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

void decode_status(uint8_t status_reg) {
    for(int i=0;i<8;i++)
    {
        int k = status_reg&(1<<i);
        if((0x01)&(k))
        {
            printf("Power On\n");
        }
        if((0b00000010)&(k))
        {
            printf("Error\n");
        }
        if((0b00000100)&(k))
        {
            printf("Tx Ready\n");
        }
        if((0b00001000)&(k))
        {
            printf("Rx Ready\n");
        }
        if((0b00010000)&(k))
        {
            printf("Overheat\n");
        }
        if((0b00100000)&(k))
        {
            printf("Undervoltage\n");
        }
        if((0b01000000)&(k))
        {
            printf("Timeout\n");
        }
        if((0b10000000)&(k))
        {
            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