Decode Status Register into Human-Readable Flags

Code

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

#define PRINT_FLAG(bit, name) \
    if ((status_reg >> bit) & 1) printf(name "\n");

void decode_status(uint8_t status_reg)
{
    PRINT_FLAG(0, "Power On");
    PRINT_FLAG(1, "Error");
    PRINT_FLAG(2, "Tx Ready");
    PRINT_FLAG(3, "Rx Ready");
    PRINT_FLAG(4, "Overheat");
    PRINT_FLAG(5, "Undervoltage");
    PRINT_FLAG(6, "Timeout");
    PRINT_FLAG(7, "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