All submissions

Control Register Using Nested Bitfields

Easy and Simple Code

#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;


int validate_register(ControlRegister *ctrl) {

    //1. Check if th4e enable is 1
    if(ctrl->bits.enable != 1) return 0;

    //2. Check the Priority
    if(ctrl->bits.priority == 3) return 0;

    //3. Reserved bits must all be 0
    if(ctrl->bits.reserved != 0) return 0; 
    
    //Else return 1
    return 1;
}

int main() {
    ControlRegister ctrl;
    scanf("%hhx", &ctrl.reg);

    int result = validate_register(&ctrl);
    printf("%d", result);

    return 0;
}

 

 

 

 

Loading...

Input

05

Expected Output

1