Validate Configuration Register Layout

Code

#include <stdio.h>

typedef struct {
    unsigned short reg;
} ConfigRegister;

typedef union {
    unsigned short reg_val;
    bool bits[16];
} Val;

int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access

    bool enON = ((cfg->reg & 1U) == 1);

    bool prior = ((cfg->reg & 0b1100)>> 2) <= 4;

    bool reserve = (cfg->reg & (((1U << 12)-1)<< 4)) == 0;
  
    return enON && prior && reserve;
}

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

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

    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

0005

Expected Output

1