#include <stdio.h>
#include <stdint.h>
typedef struct {
unsigned short reg;
} ConfigRegister;
typedef union {
struct{
uint8_t Enable : 1;
uint8_t Priority : 2;
uint8_t Mode : 1;
short Reserved : 12;
}raw_bits;
short raw;
} ConfigRegister_bits;
int validate_config(ConfigRegister *cfg) {
// Write logic using pointer access
ConfigRegister_bits ctrl_raw;
ctrl_raw.raw = cfg->reg; //access the config reg value in to the union to validate the bit positions
if((ctrl_raw.raw_bits.Enable == 0)
|| (ctrl_raw.raw_bits.Priority == 3)
|| (ctrl_raw.raw_bits.Reserved != 0))
{
return 0;
}
else
{
return 1;
}
}
int main() {
ConfigRegister cfg;
scanf("%hx", &cfg.reg);
int result = validate_config(&cfg);
printf("%d", result);
return 0;
}
Input
0005
Expected Output
1