All submissions

Decode Status Register into Human-Readable Flags

Code

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

#define BIT_PWR        0
#define BIT_ERR        1
#define BIT_TX_RDY     2
#define BIT_RX_RDY     3
#define BIT_OVH        4
#define BIT_UNDER_VLT  5
#define BIT_TO         6
#define BIT_RSV        7

#define MASK_PWR        (1 <<       BIT_PWR)
#define MASK_ERR        (1 <<       BIT_ERR)
#define MASK_TX_RDY     (1 <<    BIT_TX_RDY)
#define MASK_RX_RDY     (1 <<    BIT_RX_RDY)
#define MASK_OVH        (1 <<       BIT_OVH)
#define MASK_UNDER_VLT  (1 << BIT_UNDER_VLT)
#define MASK_TO         (1 <<        BIT_TO)
#define MASK_RSV        (1 <<       BIT_RSV)

void decode_status(uint8_t status_reg) {
    if ((status_reg & MASK_PWR)       >>       BIT_PWR) printf("Power On\n"); 
    if ((status_reg & MASK_ERR)       >>       BIT_ERR) printf("Error\n"); 
    if ((status_reg & MASK_TX_RDY)    >>    BIT_TX_RDY) printf("Tx Ready\n"); 
    if ((status_reg & MASK_RX_RDY)    >>    BIT_RX_RDY) printf("Rx Ready\n"); 
    if ((status_reg & MASK_OVH)       >>       BIT_OVH) printf("Overheat\n"); 
    if ((status_reg & MASK_UNDER_VLT) >> BIT_UNDER_VLT) printf("Undervoltage\n"); 
    if ((status_reg & MASK_TO)        >>        BIT_TO) printf("Timeout\n"); 
    if ((status_reg & MASK_RSV)       >>       BIT_RSV) printf("Reserved\n"); 
}

int main() {
    uint8_t reg;
    scanf("%hhu", &reg);
    decode_status(reg);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

13

Expected Output

Power On Tx Ready Rx Ready