Decode Status Register into Human-Readable Flags

Code

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

void zero_status() { puts("Power On"); }
void one_status() { puts("Error"); }
void two_status() { puts("Tx Ready"); }
void three_status() { puts("Rx Ready"); }
void four_status() { puts("Overheat"); }
void five_status() { puts("Undervoltage"); }
void six_status() { puts("Timeout"); }
void seven_status() { puts("Reserved"); }

typedef void (*PFV_V)(void);
PFV_V fptr_array[] = {zero_status, one_status,  two_status, three_status,
                      four_status, five_status, six_status, seven_status};

void decode_status(uint8_t status_reg) {
  // Your logic here
  size_t i = 0;
  while (i < 8) {
    if (status_reg & (1 << i)) {
      fptr_array[i]();
    }
    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