Decode Status Register into Human-Readable Flags

Code

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

const char *flags[8] = {
    "Power On",
    "Error",
    "Tx Ready",
    "Rx Ready",
    "Overheat",
    "Undervoltage",
    "Timeout",
    "Reserved"
};

void decode_status(uint8_t status_reg) {
    // Your logic here
    // 1 == 0000 0001
    // 0 == 0000 1101 -> 1
    // 1 == 0000 0110 -> 0
    // 2 == 0000 0011 -> 1
    // 3 == 0000 0001 -> 1
    // 4 == 0000 0000 -> 0 ...
    for(int pos = 0; pos < 8; pos++) {
        if(1u & (status_reg >> pos)) {
            printf("%s\n", flags[pos]);
        }
    }
}

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