All submissions

Control Register Using Nested Bitfields

Code

#include <stdio.h>

typedef union {
    unsigned char reg;
    struct {
        unsigned char Enable : 1;
        unsigned char mainode : 1;
        unsigned char Priority : 2;
        unsigned char Reserved : 4;
    } bits;
} ControlRegister;


int validate_register(ControlRegister *ctrl) {

    return (((ctrl->bits.Reserved ) == 0) && (ctrl->bits.Enable) && (ctrl->bits.Priority <=2));

}

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

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

    return 0;
}

Solving Approach

 

 

 

Loading...

Input

05

Expected Output

1