Validate Configuration Register Layout

Code

#include <stdio.h>

typedef struct {
    unsigned short reg;
} ConfigRegister;

#define ENABLE(x) ((x>>0)&0x01)
#define PRIORITY(x) ((x>>1)&0x03)
#define RESERVED(x) ((x>>4)&0xFF)

int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access
    // if(ENABLE(cfg->reg)==0x01)
    // {
    //     printf("enable on \n");
    // }
    // if(PRIORITY(cfg->reg)!=0x03)
    // {
    //     printf("valid priority \n");
    // }
    // if(RESERVED(cfg->reg)==0x00)
    // {
    //     printf("All bits 4–15 are 0 \n");
    // }
    if((ENABLE(cfg->reg)==0x01)&&(PRIORITY(cfg->reg)!=0x03)&&(RESERVED(cfg->reg)==0x00))
        return 1;
    return 0;
}

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