Validate Configuration Register Layout

Code

#include <stdio.h>
#include <stdint.h>
typedef struct {
    unsigned short reg;
} ConfigRegister;

int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access
    uint8_t enable = (cfg->reg >> 0) & 1;
    uint8_t mode = (cfg->reg) & (1 << 1);
    uint8_t pr = (cfg->reg) & (3 << 3); 
    uint8_t rsvd = (cfg->reg) & (0xFFF0);
    if((enable == 1) && (pr != 3) && (rsvd == 0)){return 1;}
    return 0;
}

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

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

    return 0;
}

/**
typedef struct {
    unsigned short enable   :1;
    unsigned short mode     :1;
    unsigned short priority :2;
    unsigned short rsvd     :12; 
}bitfields;

typedef union 
{
    
    unsigned short reg;
    bitfields reg_field;

}ConfigRegister;



int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access
    // uint8_t enable = (cfg->reg >> 0) & 1;
    // uint8_t mode = (cfg->reg) & (1 << 1);
    // uint8_t pr = (cfg->reg) & (3 << 3); 
    // uint8_t rsvd = (cfg->reg) & (0xFFF0);
    if((cfg->reg_field.enable == 1) && (cfg->reg_field.priority != 3) && (cfg->reg_field.rsvd == 0)){return 1;}

    // if((enable == 1) && (pr != 3) && (rsvd == 0)){return 1;}
    return 0;
}*/

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

0005

Expected Output

1