Decode Status Register into Human-Readable Flags

Code

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

// In embedded systems, SR often represent multiple flags using each bit.
// Each bit corresponds to a different condition.
// You must write a function that:
// Accepts a uint8_t status_reg
// Decodes which flags are set (bits = 1)
// Prints only the enabled flag names, one per line, in the order of bits from LSB to MSB (0 to 7)

void decode_status(uint8_t reg)
{
    char* flag[8] = {"Power On","Error","Tx Ready","Rx Ready","Overheat","Undervoltage","Timeout","Reserved"};
    for(uint8_t i=0; i<8; i++)
    {
        if((reg >> i)&0x01)
        {
            printf("%s\n",flag[i]); 
        }
    }
}

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