#include <stdio.h>
#include <stdint.h>
//This is an Enumeration which holds state corresponding to each bit.
typedef enum status_register
{
POWER_ON,
ERROR,
TX,
RX,
OVER_HEAT,
UNDER_VOLTAGE,
TIMEOUT,RESERVED
}s_register;
void decode_status(uint8_t reg)
{
s_register state;
int i=0;
while(i<8)
{
if(reg&(1<<i))
{
state=(s_register)i; //without typecasting it will lead to error,hence typecasted to enum type.
switch(state)
{
case POWER_ON: printf("Power On");
break;
case ERROR: printf("Error");
break;
case TX: printf("Tx Ready");
break;
case RX: printf("Rx Ready");
break;
case OVER_HEAT: printf("Overheat");
break;
case UNDER_VOLTAGE: printf("Undervoltage");
break;
case TIMEOUT: printf("Timeout");
break;
case RESERVED: printf("Reserved");
break;
}
printf("\n");
}
i++;
}
}
int main() {
uint8_t reg;
scanf("%hhu", ®);
decode_status(reg);
return 0;
}
Solving Approach
Input
13
Expected Output
Power On Tx Ready Rx Ready