77. Validate Configuration Register Layout

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

typedef struct {
    
    union{
        unsigned short reg;
        struct {
        unsigned short bit0  : 1;
        unsigned short bit1  : 1;
        unsigned short bit2  : 1;
        unsigned short bit3  : 1;
        unsigned short rest  : 12;
    };
    };

} ConfigRegister;

int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access
    if(cfg->bit0 != 1) return 0;
    if(cfg->bit2 == 1 && cfg->bit3 == 1 ) return 0;
    if(cfg->rest  != 0 ) return 0;
    return 1;
}

int main() {
    ConfigRegister cfg;
    scanf("%hx", &cfg.reg);

    int result = validate_config(&cfg);
    printf("%d", result);

    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote