#include <stdio.h>
#include <stdint.h>
void decode_status(uint8_t reg) {
char *arr[] = {
"Power On", // Index 0
"Error", // Index 1
"Tx Ready", // Index 2
"Rx Ready", // Index 3
"Overheat", // Index 4
"Undervoltage", // Index 5
"Timeout", // Index 6
"Reserved" // Index 7
};
int is_first = 1; // Set our flag to "true" (1)
for(uint8_t i = 0; i < 8; i++) {
if(reg & 1U) {
// If it is NOT the first item, print a newline to separate it
// from the previous item.
if (!is_first) {
printf("\n");
}
// Print the string completely clean (no spaces, no \n)
printf("%s", arr[i]);
// Turn the flag off. Every subsequent item will now print a \n first.
is_first = 0;
}
reg = reg >> 1;
}
}
int main() {
uint8_t reg;
if (scanf("%hhu", ®) == 1) {
decode_status(reg);
}
return 0;
}Solving Approach
Input
13
Expected Output
Power On Tx Ready Rx Ready