67. Control Register Using Nested Bitfields

Back To All Submissions
Previous Submission
Next Submission

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;

// Write your logic here
int validate_register(ControlRegister *ctrl) {
    int hehe =0;
/*    if (((ctrl->reg >> 4)& 0x0F) ==0)
    {   
        if(((ctrl->reg >> 2) & 0x03) <=2)
        {
            if (((ctrl->reg >> 0) & 0x01) ==1) return 1;
        }
    } */
hehe =(ctrl->bits.enable != 1)? (0) : ((ctrl->bits.priority >2)? ( 0) : ((ctrl->bits.reserved != 0)? ( 0) : ( 1)));
return hehe;

    
}

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

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

    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote