#include <stdio.h>
typedef union {
unsigned char reg;
struct {
unsigned char enable : 1;
unsigned char mode : 1;
unsigned char priority : 2;
unsigned char reserved : 4;
} bits;
} ControlRegister;
// Write your logic here
int validate_register(ControlRegister *ctrl) {
/* unsigned char enable = (ctrl->bits.enable) == 1;
unsigned char priority = (ctrl->bits.priority) <= 2;
unsigned char reserved = (ctrl->bits.reserved) == 0;*/
/* ANOHERT SOLUTION BY USING REG ONLY NOT BIT FIELD */
unsigned char reg = ctrl->reg ;
unsigned char enable = (reg & 1) == 1;
unsigned char priority = ((reg >> 2)&3) <= 2;
unsigned char reserved = (reg & 0xF0) == 0;
return (enable && priority && reserved) ? 1 : 0;
}
int main() {
ControlRegister ctrl;
scanf("%hhx", &ctrl.reg);
int result = validate_register(&ctrl);
printf("%d", result);
return 0;
}
Input
05
Expected Output
1