#include <stdio.h>
#include <stdint.h>
typedef union{
uint8_t input;
struct{
unsigned BIT_SET_0 :1;
unsigned BIT_SET_1 :1;
unsigned BIT_SET_2 :1;
unsigned BIT_SET_3 :1;
unsigned BIT_SET_4 :1;
unsigned BIT_SET_5 :1;
unsigned BIT_SET_6 :1;
unsigned BIT_SET_7 :1;
}_8bit;
}data;
void decode_status(uint8_t status_reg) {
data r;
r.input = status_reg;
if(r._8bit.BIT_SET_0==1)
{
printf("Power On\n");
}
if(r._8bit.BIT_SET_1==1)
{
printf("Error\n");
}
if(r._8bit.BIT_SET_2==1)
{
printf("Tx Ready\n");
}
if(r._8bit.BIT_SET_3==1)
{
printf("Rx Ready\n");
}
if(r._8bit.BIT_SET_4==1)
{
printf("Overheat\n");
}
if(r._8bit.BIT_SET_5==1)
{
printf("Undervoltage\n");
}
if(r._8bit.BIT_SET_6==1)
{
printf("Timeout\n");
}
if(r._8bit.BIT_SET_7==1)
{
printf("Reserved\n");
}
}
int main() {
uint8_t reg;
scanf("%hhu", ®);
decode_status(reg);
return 0;
}Solving Approach
Input
13
Expected Output
Power On Tx Ready Rx Ready