#include <stdio.h>
#include <stdint.h>
char status[8][20] = {"Power On", "Error", "Tx Ready",
"Rx Ready", "Overheat","Undervoltage",
"Timeout","Reserved"};
void decode_status(uint8_t status_reg) {
for (int bit = 0; bit <8; bit++){
if (((status_reg & (1<<bit))>>bit) == 1){
printf("%s\n", status[bit]);
}
}
}
int main() {
uint8_t reg;
scanf("%hhu", ®);
decode_status(reg);
return 0;
}
Solving Approach
We need to loop through each bit in the status register and check which bits are by using the AND & operator and right shift it to read the bit. Because we have to print flag name, we will store the names in a string array and print according to the bit and array index.