Decode Status Register into Human-Readable Flags

Code

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

#define PowerOn      (1<<0)
#define Error        (1<<1)
#define TxReady      (1<<2)
#define RxReady      (1<<3)
#define Overheat     (1<<4)
#define Undervoltage (1<<5)
#define Timeout      (1<<6)
#define Reserved     (1<<7)


void decode_status(uint8_t status_reg) {
    // Your logic here
    
    char *string[8]={"Power On","Error","Tx Ready","Rx Ready","Overheat","Undervoltage","Timeout","Reserved"};
    int n=0;
    while(n<8)
    {
        if((status_reg>>n)&1) printf("%s\n",string[n]);
        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