77. Validate Configuration Register Layout

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>
#include <stdint.h>

typedef union {
    struct {
        uint16_t EN          : 1;
        uint16_t MODE        : 1;
        uint16_t PRIORITY    : 2;
        uint16_t RESERVED    : 12; // reserved
    } bits;
    unsigned short reg;
} ConfigRegister;


int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access
    if(cfg->bits.EN == 0)
    {
        return 0;
    }
    if(cfg->bits.PRIORITY > 2)
    {
        return 0;
    }
    if(cfg->bits.RESERVED != 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