#include <stdio.h>
#include <stdint.h>
static char *flag_status_meaning[8] = {"Power On", "Error", "Tx Ready", "Rx Ready", "Overheat", "Undervoltage",
"Timeout", "Reserved"};
void decode_status(uint8_t status_reg) {
// Your logic here
uint8_t flag_status = status_reg;
for(int i = 0; i < 8; ++i)
{
if((flag_status & 1U))
{
printf("%s\n", flag_status_meaning[i]);
}
flag_status >>= 1U;
}
}
int main() {
uint8_t reg;
scanf("%hhu", ®);
decode_status(reg);
return 0;
}
Solving Approach
Req:
- map the flags to the bit position
- return what is set
0 - 255
0 -> return nothing
255 -> all the flags are set
Plan:
- keep shifting to the right and take & with 1U
- if the above is 1 then as per the iteration print the flag
- if not then skip or print nothing
Input
13
Expected Output
Power On Tx Ready Rx Ready