77. Validate Configuration Register Layout

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>
#include <stdint.h>
typedef struct {
    unsigned short reg;
} ConfigRegister;
typedef union {
    unsigned short val;
    struct{
        unsigned short ena:1;
        unsigned short mod:1;
        unsigned short prio:2;
        unsigned short res:12;
    }bits;
}frame;
int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access
    frame f;
    
    f.val = cfg->reg;
    if(f.bits.ena != 1)
    {
        return 0;
    }
    if(f.bits.prio == 3)
    {
        return 0;
    }
    if(f.bits.res != 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