Decode Status Register into Human-Readable Flags

Code

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


typedef union{

    uint8_t reg;

    struct{
    uint8_t power_on:1;
    uint8_t error:1;
    uint8_t tx_ready:1;
    uint8_t rx_ready:1;
    uint8_t overheat:1;
    uint8_t undervoltage:1;
    uint8_t timeout:1;
    uint8_t reserved:1;

    }bits;

    }STATUS;
void decode_status(uint8_t status_reg) {
    // Your logic here
    STATUS cond;
    cond.reg=status_reg;

    if(cond.bits.power_on==1){
        printf("Power On\n");
    }

    if(cond.bits.error==1){
        printf("Error\n");
    }

    if(cond.bits.tx_ready==1){
        printf("Tx Ready\n");
    }

    if(cond.bits.rx_ready==1){
        printf("Rx Ready\n");
    }

    if(cond.bits.overheat==1){
        printf("Overheat\n");
    }

    if(cond.bits.undervoltage==1){
        printf("Undervoltage\n");
    }

    if(cond.bits.timeout==1){
        printf("Timeout\n");
    }

    if(cond.bits.reserved==1){
        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