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